Service优化

1.Service优化思路
2.进程优先级
3.如何不被系统轻易杀死:提高进程优先级

  • QQ的例子:一个像素的Activity

4.如何防止被杀死

  • 1.系统白名单
  • 2.双进程守护
  • 3.JobSchedule
  • 4.应用监听
  • 5.账号同步
  • 6.NDK解决:双进程守护

1.Service优化思路:

目标是让应用减少占内存,减少耗电量

service:是一个后台服务,专门用来处理常驻后台的工作的组件。处理比较耗时的处理,比如上传图片,心跳监听。

例子:

即时通讯:service来做常驻后台的心跳传输。

考虑:
1.良民:核心服务尽可能地轻!!!,比如微信有好几个进程
很多人喜欢把所有的后台操作都集中在一个service里面。
为核心服务专门做一个进程,跟其他的所有后台操作隔离。树大招风,核心服务千万要轻。

2.进程优先级

进程的重要性优先级:(越往后的就越容易被系统杀死)

进程 例子
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设计的,为了第二次启动更快,采取的一个权衡)

3.如何提升进程的优先级(尽量做到不轻易被系统杀死)

3.1.QQ的例子

QQ采取在锁屏的时候启动一个1个像素的Activity,当用户解锁以后将这个Activity结束掉(顺便同时把自己的核心服务再开启一次)。被用户发现了就不好了。
故事:小米撕逼。
背景:当手机锁屏的时候什么都干死了,为了省电。锁屏界面在上面盖住了。
思路:

监听锁屏广播,锁了---启动这个Activity。
监听锁屏的, 开启---结束掉这个Activity。
要监听锁屏的广播---动态注册。

public class KeepLiveActivity extends Activity {

    private static final String TAG = "ricky";

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

    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();
    }
}
public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        ScreenListener listener = new ScreenListener(this);
        listener.begin(new ScreenStateListener() {
            
            @Override
            public void onUserPresent() {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onScreenOn() {
                // 开屏---finish这个一个像素的Activity
                KeepLiveActivityManager.getInstance(MyService.this).finishKeepLiveActivity();
            }
            
            @Override
            public void onScreenOff() {
                // 锁屏---启动一个像素的Activity
//              startActivity(intent);
                KeepLiveActivityManager.getInstance(MyService.this).startKeepLiveActivity();
            }
        });
        
    }

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

4.如何做到进程不被系统杀死。

4.1.app运营商和手机厂商可能有合作关系---白名单。

4.2.双进程守护

可以防止单个进程杀死,同时可以防止第三方的360清理掉。
一个进程被杀死,另外一个进程又被他启动。相互监听启动。

A<--->B
杀进程是一个一个杀的。本质是和杀进程时间赛跑。

例子:

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

}

AIDL进行两个进程间通信

package com.dn.keepliveprocess;
interface RemoteConnection{
    String getProcessName();
}

A进程服务:

public class LocalService extends Service {

    public static final String TAG = "ricky";
    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) {
        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);
        }
        
    }
    
}

B进程服务:

public class RemoteService extends Service {

    public static final String TAG = "ricky";
    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);
        }
        
    }
    
    
}

注册服务:



4.3.JobSchedule

把任务加到系统调度队列中,当到达任务窗口期的时候就会执行,我们可以在这个任务里面启动我们的进程。
这样可以做到将近杀不死的进程。

@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.dn.keepliveprocess.LocalService");
        boolean isRemoteServiceWork = isServiceWork(this, "com.dn.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 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;  
    }  
}

注册服务和权限

 
 //开机监听

4.4.监听其他应用

监听QQ,微信,系统应用,友盟,小米推送等等的广播,然后把自己启动了。

4.5.利用账号同步机制唤醒我们的进程

AccountManager

4.6.NDK来解决,Native进程来实现双进程守护。

总结:要根据自己的需要来使用。

你可能感兴趣的:(Service优化)