Android-Service的保活方法

作者: TryEnough | 来源:发表于2019-03-07 17:08 被阅读19次

支持原文: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

相关文章

  • Android-Service的保活方法

    支持原文:http://tryenough.com/android-service-life 保活Service可...

  • iOS后台保活

    iOS后台保活按时间可分为短时保活和长时间保活 短时保活的方式通过beginBackgroundTaskWithN...

  • Android如何保活指导文档

    因为工作原因需要程序一直后台运行,整理了一下后台保活相关方法。留此存档 保活和省电的矛盾 Android 设备运行...

  • 安卓APP后台保活机制探索

    保活,分为两个部分:网络连接保活,进程保活。 网络保活,业界主要手段有: a. GCM b. 公共的第三方push...

  • 深度剖析APP保活案例

    这是作者在去年处理的一个关于进程保活的案例 一. 引言 1.1 保活概述 什么是保活?保活就是在用户主动杀进程,或...

  • iOS app进入后台后 应用保活 后台保活

    iOS app进入后台后 应用保活 后台保活

  • Android中“强制停止”和广播保活的一个小坑

    转载注明出处:简书-十个雨点 一直以来,使用广播进行Android进程的保活就是一种常规的保活方法,本着用事实说话...

  • Android常见进程保活方法

    一、Android进程优先级 前台进程 ——Foreground process用户正在使用的进程,一般系统不会杀...

  • 进程保活

    保活手段 当前业界的Android进程保活手段主要分为 黑、白、灰 三种,其大致的实现思路如下: 黑色保活:不同的...

  • 进程保活与拉活

    进程相关知识梳理 Activity 1像素保活 前台服务保活 账户同步拉活 JobScheduler 拉活 双进程...

网友评论

    本文标题:Android-Service的保活方法

    本文链接:https://www.haomeiwen.com/subject/urkhpqtx.html