杀不死的服务

后台服务

有个需求,要在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);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值