欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

性能优化二十三之Service进程防杀死

程序员文章站 2022-07-02 11:03:22
...

前言

        Service作为四大组件之一很多安卓开发者对他肯定是又爱又恨,爱在它可以帮助我们去处理常驻后台的任务操作,比如说推送服务,心跳传输等,恨在当Servcie遇到360、腾讯管家之类的杀毒软件,变得脆弱,很容易被杀死,或者当用户手机锁屏会导致服务暂时停止运行。
        我们常用的进程守护的套路无外乎以下几种:
一、提高进程的优先级数值
        对于Service被系统回收,一般做法是通过提高优先级可以解决,在AndroidManifest.xml文件中对于intent-filter可以通过Android:priority = “1000”这个属性设置最高优先级,1000是最高值,如果数字越小则优先级越低。
二、改变进程等级

进程的重要性优先级:(越往后的就越容易被系统杀死)
1.前台进程;Foreground process
    1)用户正在交互的Activity(onResume())
    2)当某个Service绑定正在交互的Activity。
    3)被主动调用为前台Service(startForeground())
    4)组件正在执行生命周期的回调(onCreate()/onStart()/onDestroy())
    5)BroadcastReceiver 正在执行onReceive();

2.可见进程;Visible process
    1)我们的Activity处在onPause()(没有进入onStop())
    2)绑定到前台Activity的Service。

3.服务进程;Service process
    简单的startService()启动。
4.后台进程;Background process
    对用户没有直接影响的进程----Activity出于onStop()的时候。
    android:process=":xxx"
5.空进程; Empty process
    不含有任何的活动的组件。(android设计的,为了第二次启动更快,采取的一个权衡)

三、设置 android:persistent属性为true

< application 
    android:name="com.test.Application" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:persistent="true"</span> 
    android:theme="@style/AppTheme" > 
< /application> 

        这个属性设置后,的确发现优先级提高不少,或许是相当于系统级的进程,但是还是无法保证存活。

四、onDestory方法内调用广播,重启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>  

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

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);  
        }  

    }  

}  

        但是当使用类似于360卫士和腾讯管家等第三方应用或是在setting里-应用-强制停止时,APP进程可能就直接被干掉了,onDestroy方法都进不来,所以还是无法保证存活。
五、监听系统广播判断Service状态
通过系统的一些广播,比如:手机重启、界面唤醒、应用状态改变等等监听并捕获到,然后判断我们的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); 
    } 
} 

        这个方法监听多了会导致Service很混乱,带来诸多麻烦。

六、QQ采取在锁屏的时候启动一个1个像素的Activity,当用户解锁以后将这个Activity结束掉(顺便同时把自己的核心服务再开启一次,接下来就来模拟实现:

//主界面Activity
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }

    public void jump(View v){
        Intent intent = new Intent(this, KeepLiveActivity.class);
        startActivity(intent);
        finish();
    }
}

//1个像素的activity
public class KeepLiveActivity extends Activity {
    private static final String TAG = "kiven";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      setContentView(R.layout.activity_main);
        Log.i(TAG, "KeepLiveActivity----onCreate!!!");
        Window window = getWindow();
        window.setGravity(Gravity.LEFT|Gravity.TOP);
        LayoutParams params = window.getAttributes();
        params.height = 1;
        params.width = 1;
        params.x = 0;
        params.y = 0;   
        window.setAttributes(params);
        KeepLiveActivityManager.getInstance(this).setKeepLiveActivity(this);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.i(TAG, "KeepLiveActivity----onDestroy!!!");
    }   
}

//管理服务的开启与关闭
public class KeepLiveActivityManager {
    private static KeepLiveActivityManager instance;
    private Context context;
    private WeakReference<Activity> activityInstance;

    public static KeepLiveActivityManager getInstance(Context context) {
        if(instance==null){
            instance = new KeepLiveActivityManager(context.getApplicationContext());
        }
        return instance;
    }

    private KeepLiveActivityManager(Context context) {
        this.context = context;
    }

    public void setKeepLiveActivity(Activity activity){
        activityInstance = new WeakReference<Activity>(activity);
    }

    public void startKeepLiveActivity() {
        Intent intent = new  Intent(context, KeepLiveActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    public void finishKeepLiveActivity() {
        if(activityInstance!=null&&activityInstance.get()!=null){
            Activity activity = activityInstance.get();
            activity.finish();
        }
    }

}

//屏幕状态监听
public class ScreenListener {
    private Context mContext;
    private ScreenBroadcastReceiver mScreenReceiver;
    private ScreenStateListener mScreenStateListener;

    public ScreenListener(Context context) {
        mContext = context;
        mScreenReceiver = new ScreenBroadcastReceiver();
    }

    /**
     * screen状态广播接收者
     */
    private class ScreenBroadcastReceiver extends BroadcastReceiver {
        private String action = null;
        @Override
        public void onReceive(Context context, Intent intent) {
            action = intent.getAction();
            if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏
                mScreenStateListener.onScreenOn();
            } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏
                mScreenStateListener.onScreenOff();
            } else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁
                mScreenStateListener.onUserPresent();
            }
        }
    }

    /**
     * 开始监听screen状态
     * 
     * @param listener
     */
    public void begin(ScreenStateListener listener) {
        mScreenStateListener = listener;
        registerListener();
        getScreenState();
    }

    /**
     * 获取screen状态
     */
    private void getScreenState() {
        PowerManager manager = (PowerManager) mContext
                .getSystemService(Context.POWER_SERVICE);
        if (manager.isScreenOn()) {
            if (mScreenStateListener != null) {
                mScreenStateListener.onScreenOn();
            }
        } else {
            if (mScreenStateListener != null) {
                mScreenStateListener.onScreenOff();
            }
        }
    }

    /**
     * 停止screen状态监听
     */
    public void unregisterListener() {
        mContext.unregisterReceiver(mScreenReceiver);
    }

    /**
     * 启动screen状态广播接收器
     */
    private void registerListener() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        mContext.registerReceiver(mScreenReceiver, filter);
    }

    public interface ScreenStateListener {// 返回给调用者屏幕状态信息
        public void onScreenOn();

        public void onScreenOff();

        public void onUserPresent();
    }


    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT"/>
}

        上面实现了1像素保活,当接受到监听锁屏广播,就会启动这个1个像素的Activity,当监听界面唤醒广播,就结束掉这个1个像素的Activity,要监听锁屏的广播需要使用动态注册的方式(实际项目中不会出现静态注册广播的方式)。
七:双进程守护(重点)
        如何让系统或者第三方应用无法杀死进程,除了上面的六种方式,还有的就是一般手机厂商会与app运营商达成合作,加入到白名单,就不会被杀死。这种方式我们一般是很难用的。但是另一种方式双进程守护方式来实现,什么是双进程守护?双进程守护—可以防止单个进程杀死,同时可以防止第三方的360清理掉。一个进程被杀死,另外一个进程又被他启动。相互监听启动。双进程的互相唤醒,就相当于双进程之间要进行互相通信,进程间的通信是通过binder机制,所以这两个进程之间可以通过aidl来实现。

RemoteConnection.aidl
interface RemoteConnection{
    String getProcessName();
}

LocalService.java 本地服务
public class LocalService extends Service {

    public static final String TAG = "kiven";
    private MyBinder binder;
    private MyServiceConnection conn;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if(binder ==null){
            binder = new MyBinder();
        }
        conn = new MyServiceConnection();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);

        PendingIntent contentIntent = PendingIntent.getService(this, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setTicker("360")
        .setContentIntent(contentIntent)
        .setContentTitle("我是360")
        .setAutoCancel(true)
        .setContentText("hehehe")
        .setWhen(System.currentTimeMillis());

        //把service设置为前台运行,避免手机系统自动杀掉改服务。
        startForeground(startId, builder.build());
        return START_STICKY;
    }


    class MyBinder extends RemoteConnection.Stub{

        @Override
        public String getProcessName() throws RemoteException {
            // TODO Auto-generated method stub
            return "LocalService";
        }

    }

    class MyServiceConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "建立连接成功!");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "RemoteService服务被干掉了~~~~断开连接!");
            Toast.makeText(LocalService.this, "断开连接", 0).show();
            //启动被干掉的
            LocalService.this.startService(new Intent(LocalService.this, RemoteService.class));
            LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);
        }   
    }
}

RemoteService.java: 远程服务(另一个进程)
public class RemoteService extends Service {

    public static final String TAG = "kiven";
    private MyBinder binder;
    private MyServiceConnection conn;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return binder;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        if(binder ==null){
            binder = new MyBinder();
        }
        conn = new MyServiceConnection();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);

        PendingIntent contentIntent = PendingIntent.getService(this, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setTicker("360")
        .setContentIntent(contentIntent)
        .setContentTitle("我是360")
        .setAutoCancel(true)
        .setContentText("hehehe")
        .setWhen( System.currentTimeMillis());

        //把service设置为前台运行,避免手机系统自动杀掉改服务。
        startForeground(startId, builder.build());
        return START_STICKY;
    }

    class MyBinder extends RemoteConnection.Stub{

        @Override
        public String getProcessName() throws RemoteException {
            // TODO Auto-generated method stub
            return "LocalService";
        }

    }

    class MyServiceConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "建立连接成功!");

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "LocalService服务被干掉了~~~~断开连接!");
            Toast.makeText(RemoteService.this, "断开连接", 0).show();
            //启动被干掉的
            RemoteService.this.startService(new Intent(RemoteService.this, LocalService.class));
            RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);
        }   
    }
}


MainActivity.java:
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this, LocalService.class));
        startService(new Intent(this, RemoteService.class));
        startService(new Intent(this, JobHandleService.class));
    }

}

JobHandleService.java: JobHandlerService的作用是为了防止应用被强制关闭导致服务被杀(实际开发不建议使用,太流氓了)
@SuppressLint("NewApi")
public class JobHandleService extends JobService{
    private int kJobId = 0;
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("INFO", "jobService create");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("INFO", "jobService start");
        scheduleJob(getJobInfo());
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        // TODO Auto-generated method stub
        Log.i("INFO", "job start");
//      scheduleJob(getJobInfo());
        boolean isLocalServiceWork = isServiceWork(this, "com.kiven.keepliveprocess.LocalService");
        boolean isRemoteServiceWork = isServiceWork(this, "com.kiven.keepliveprocess.RemoteService");
//      Log.i("INFO", "localSericeWork:"+isLocalServiceWork);
//      Log.i("INFO", "remoteSericeWork:"+isRemoteServiceWork);
        if(!isLocalServiceWork||
           !isRemoteServiceWork){
            this.startService(new Intent(this,LocalService.class));
            this.startService(new Intent(this,RemoteService.class));
            Toast.makeText(this, "process start", Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.i("INFO", "job stop");
//      Toast.makeText(this, "process stop", Toast.LENGTH_SHORT).show();
        scheduleJob(getJobInfo());
        return true;
    }

    /** Send job to the JobScheduler. */
    public void scheduleJob(JobInfo t) {
        Log.i("INFO", "Scheduling job");
        JobScheduler tm =
                (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        tm.schedule(t);
    }

    public JobInfo getJobInfo(){
        JobInfo.Builder builder = new JobInfo.Builder(kJobId++, new ComponentName(this, JobHandleService.class));
        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        builder.setPersisted(true);
        builder.setRequiresCharging(false);
        builder.setRequiresDeviceIdle(false);
        builder.setPeriodic(10);//间隔时间--周期
        return builder.build();
    }


    /** 
     * 判断某个服务是否正在运行的方法 
     *  
     * @param mContext 
     * @param serviceName 
     *            是包名+服务的类名(例如:net.loonggg.testbackstage.TestService) 
     * @return true代表正在运行,false代表服务没有正在运行 
     */  
    public boolean isServiceWork(Context mContext, String serviceName) {  
        boolean isWork = false;  
        ActivityManager myAM = (ActivityManager) mContext  
                .getSystemService(Context.ACTIVITY_SERVICE);  
        List<RunningServiceInfo> myList = myAM.getRunningServices(100);  
        if (myList.size() <= 0) {  
            return false;  
        }  
        for (int i = 0; i < myList.size(); i++) {  
            String mName = myList.get(i).service.getClassName().toString();  
            if (mName.equals(serviceName)) {  
                isWork = true;  
                break;  
            }  
        }  
        return isWork;  
    }  
}

AndroidManifest.xml:
....    
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
...

   <service android:name="com.kiven.keepliveprocess.LocalService"></service>
   <service 
        android:name="com.kiven.keepliveprocess.RemoteService"
        android:process=":remoteprocess"
       ></service>
    <service android:name="com.kiven.keepliveprocess.JobHandleService" android:permission="android.permission.BIND_JOB_SERVICE"></service>
    </application>

</manifest>

       至此,进程保活的方式介绍完毕,实际上进程保护有很多种方式,需要根据实际的需求去选择适合的方式,一个小建议:不要把app搞的太”流氓”,会导致用户手机用电量飙升,CPU发热,也对用户的体验不好。

相关标签: 性能优化