Android-Service的保活方法

支持原文:http://tryenough.com/android-service-life

保活Service可从两方面考虑:

一.改变Service自身的方法

1.提高Service的优先级

在AndroidManifest.xml文件中对于intent-filter可以通过android:priority = "1000"这个属性设置最高优先级,1000是最高值,如果数字越小则优先级越低,同时适用于广播。

        <service
            android:name="com.dbjtech.acbxt.waiqin.UploadService"
            android:enabled="true" >
            <intent-filter android:priority="1000" >
                <action android:name="com.dbjtech.myservice" />
            </intent-filter>
        </service>
复制代码

2.在Service即将销毁的时候重新启动

支持原文:http://tryenough.com/android-service-life

可以直接在onDestroy()里startService

	@Override
	public void onDestroy() {
 
		 Intent sevice = new Intent(this, MainService.class);
		 this.startService(sevice);
 
		super.onDestroy();
	}
复制代码

也可以用service +broadcast 方式启动:

onDestroy方法里重启service,当service走ondestory的时候,发送一个自定义的广播,当收到广播的时候,重新启动service;

        <receiver android:name="com.dbjtech.acbxt.waiqin.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="com.dbjtech.waiqin.destroy" />//这个就是自定义的action
            </intent-filter>
        </receiver>
复制代码

在onDestroy时:

	@Override
	public void onDestroy() {
		stopForeground(true);
		Intent intent = new Intent("com.dbjtech.waiqin.destroy");
		sendBroadcast(intent);
		super.onDestroy();
	}
复制代码

在BootReceiver里

支持原文:http://tryenough.com/android-service-life

public class BootReceiver extends BroadcastReceiver {
 
	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals("com.dbjtech.waiqin.destroy")) {
			//TODO
			//在这里写重新启动service的相关操作
				startUploadService(context);
		}
 
	}
 
}
复制代码

3.onStartCommand方法,返回START_STICKY

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		flags = START_STICKY;
		return super.onStartCommand(intent, flags, startId);
	}
复制代码

4.提升service进程优先级 在onStartCommand方法内添加如下代码:

		 Notification notification = new Notification(R.drawable.ic_launcher,
		 getString(R.string.app_name), System.currentTimeMillis());
		
		 PendingIntent pendingintent = PendingIntent.getActivity(this, 0,
		 new Intent(this, AppMain.class), 0);
		 notification.setLatestEventInfo(this, "uploadservice", "请保持程序在后台运行",
		 pendingintent);
		startForeground(0x111, notification);
复制代码

二.利用系统特性的方法

支持原文:http://tryenough.com/android-service-life

1.监听系统特殊事件

通过系统的一些广播,比如:手机重启、界面唤醒、应用状态改变等等监听并捕获到,然后判断我们的Service是否还存活,别忘记加权限啊。

        <receiver android:name="com.dbjtech.acbxt.waiqin.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="android.intent.action.PACKAGE_RESTARTED" />
                <action android:name="com.dbjtech.waiqin.destroy" />
            </intent-filter>
        </receiver>
复制代码

BroadcastReceiver中:

	@Override
	public void onReceive(Context context, Intent intent) {
		if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
			System.out.println("手机开机了....");
			startUploadService(context);
		}
		if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
				startUploadService(context);
		}
	}
复制代码

2.特殊手机监听特殊推送,例如小米手机注册小米推送

支持原文:http://tryenough.com/android-service-life

转载于:https://juejin.im/post/5c80dee4e51d45282610e2cb

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值