后台服务
有个需求,要在android设备里面内置app,将设备信息定时上传服务端。
杀不死的服务
做了几次修改,从开始的启动设备一天app就停止服务,到现在测试一个星期没有出现问题。show code
- 守护进程
<service
android:name=".GuardService"
android:process=":GuardService">
<intent-filter>
<!-- 系统启动完成后会调用-->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.DATE_CHANGED"/>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="android.intent.action.ACTION_TIME_TICK"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</service>
- 后台工作进程
<!--后台工作服务-->
<service
android:name=".SlptClockService"
android:process="main.work">
<intent-filter android:priority="1000">
<!-- 系统启动完成后会调用-->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.DATE_CHANGED"/>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="android.intent.action.ACTION_TIME_TICK"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</service>
GuardService.java
public class GuardService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new ProcessConnection.Stub() {};
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(1, new Notification());
//绑定建立链接
bindService(new Intent(this, SlptClockService.class), mServiceConnection, Context.BIND_IMPORTANT);
return START_STICKY;
}
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//链接上
Log.d("test", "GuardService:建立链接");
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
startService(new Intent(GuardService.this, SlptClockService.class));
//重新绑定
bindService(new Intent(GuardService.this, SlptClockService.class),
mServiceConnection, Context.BIND_IMPORTANT);
}
};
}
- 工作进程中主要是在启动时开启定时任务
- onCreate方法里面绑定服务
SlptClockService.java
bindService(new Intent(SlptClockService.this, GuardService.class), connection, Context.BIND_ABOVE_CLIENT);
private ServiceConnection connection = new ServiceConnection() {
/**
* 在终止后调用,我们在杀死服务的时候就会引起意外终止,就会调用onServiceDisconnected
* 则我们就得里面启动被杀死的服务,然后进行绑定
*/
@Override
public void onServiceDisconnected(ComponentName name) {
//守护进程被杀死了,重启
Intent remoteService = new Intent(SlptClockService.this, GuardService.class);
startService(remoteService);
bindService(new Intent(SlptClockService.this, GuardService.class),
connection, Context.BIND_ABOVE_CLIENT);
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
};
- 在onStartCommand方法中发送定时消息
/**
* 发送唤醒广播来促使挂掉进程重新启动起来
*/
private void wakeUpAlarm() {
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, ReciveMsgReciver.class);
i.setAction("wake");
LogUtil.e("onReqSuccess", "发送定时任务 【wake】");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 60, pi);
}