android 后台服务长时间运行---解决方案

来源:互联网 发布:黑道圣徒4数据碎片代码 编辑:程序博客网 时间:2024/05/17 04:22

android 后台服务启动方式:

第一种:Activity界面通过Intent启动相关service.====等价于(Timer定时器+TimerTask定时任务)

第二种:Activity界面通过发生广播启动相关service=====等价于(AlarmManager闹钟服务)

 

相关示列代码:待补全

 

android 后台服务如何长时间运行解决办法:

解决后台服务长时间运行第一种和第二种方案,这里就不贴出相关的配置文件信息,我们将重点讲解第三种和第四种解决方案。

 

第三种:解决方案

重写Service的onStartCommand方法,使用StartForeground(int,Notification)方法来启动service。

注:前台服务会在状态栏显示一个通知,最典型的应用就是音乐播放器,只要在播放状态下,就算休眠也不会被杀,如果不想显示通知,只要把参数里的int设为0即可。

Java核心代码:

Notification notification = new Notification(R.drawable.logo,"wf update service is running",                 System.currentTimeMillis()); 
pintent=PendingIntent.getService(this, 0, intent, 0);  
notification.setLatestEventInfo(this, "WF Update Service","wf update service is running!", pintent); 
//让该service前台运行,避免手机休眠时系统自动杀掉该服务  
//如果 id 为 0 ,那么状态栏的 notification 将不会显示。  
startForeground(startId, notification); 

同时,对于通过startForeground启动的service,onDestory方法中需要通过stopForeground(true)来取消前台运行状态。

ps:如果service被杀后下次重启出错,可能是此时重发的Intent为null的缘故,可以通过修改onStartCommand方法的返回值来解决:

START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。

START_NOT_STICKY:“非粘性的”。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务。

START_REDELIVER_INTENT:重传Intent。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会自动重启该服务,并将Intent的值传入。

START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保证服务被kill后一定能重启。

 

第四种:解决方案

1、Intent.ACTION_TIME_TICK的使用
我们知道广播的注册有静态注册和动态注册,但此系统广播只能通过动态注册的方式使用。即你不能通过在manifest.xml里注册的方式接收到这个广播,只能在代码里通过registerReceiver()方法注册。

在ThisApp extends Application 或者在service里注册广播:

IntentFilter filter = newIntentFilter(Intent.ACTION_TIME_TICK);         MyBroadcastReceiver receiver = new MyBroadcastReceiver();     registerReceiver(receiver, filter); 


在广播接收器MyBroadcastReceiver extends BroadcastReceiver的onReceive里

 boolean isServiceRunning = false;     if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {           //检查Service状态           ActivityManager manager = (ActivityManager)ThisApp.getContext().getSystemService(Context.ACTIVITY_SERVICE);     for (RunningServiceInfo service :manager.getRunningServices(Integer.MAX_VALUE)) {     if("so.xxxx.xxxxService".equals(service.service.getClassName()))                {      isServiceRunning = true;     }            }     if (!isServiceRunning) {     Intent i = new Intent(context, xxxService.class);            context.startService(i);     } } 




 

0 0