两个服务的方式实现双进程守护
2016-10-11 10:54
387 查看
其实双进程守护,保证我们的应用不会被安全软件杀死,的关键在于我们使用了两个进程,并且是他们是相互绑定的,但是测试发现这种做法,对于小米手机来说,并不起作用。我听说有人使用同步数据的方式来唤醒死掉的进程,或者是使用NDK方式来保护我们的应用,这两种方式,下次再分享。
远程服务
package com.example.mypc.doubleprocess;import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
/**
* Created by MyPC on 2016/10/9.
*/
public class RemoteService extends Service {
private Binder mBinder; private MyConn mConn; @Nullable @Override public IBinder onBind(Intent intent) { return mBinder; } @Override public void onCreate() { mBinder=new MyBinder(); if(mConn==null){ mConn=new MyConn(); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { //第一次绑定对方。 RemoteService.this.bindService(new Intent(this,LocalService.class),mConn,BIND_IMPORTANT); return super.onStartCommand(intent, flags, startId); } class MyBinder extends IProcessService.Stub{ public String getServiceName(){ return "RemoteService"; } } class MyConn implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i("TAG","远程链接本地服务成功"); Toast.makeText(RemoteService.this,"链接本地服务成功",Toast.LENGTH_LONG).show(); } @Override public void onServiceDisconnected(ComponentName name) { //对方(localService)被干掉了 Log.i("TAG","localService服务被杀"); Toast.makeText(RemoteService.this,"localService服务被杀",Toast.LENGTH_LONG).show(); //启动对方(对方会走onStart绑定我) RemoteService.this.startService(new Intent(RemoteService.this,LocalService.class)); //绑定对方(我再绑定对方) RemoteService.this.bindService(new Intent(RemoteService.this,LocalService.class),mConn, Context.BIND_IMPORTANT); } }
}
本地服务
package com.example.mypc.doubleprocess;import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
impor
4000
t android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
/**
* Created by MyPC on 2016/10/9.
*/
public class LocalService extends Service {
private Binder mBinder;
private MyConn mConn;
@Nullable @Override public IBinder onBind(Intent intent) { return mBinder; } @Override public void onCreate() { mBinder=new MyBinder(); if(mConn==null){ mConn=new MyConn(); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { //第一次绑定对方。 //再第二次还会绑定一次 LocalService.this.bindService(new Intent(this,RemoteService.class),mConn,BIND_IMPORTANT); return super.onStartCommand(intent, flags, startId); } class MyBinder extends IProcessService.Stub{ public String getServiceName(){ return "LocalService"; } } class MyConn implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i("TAG","本地链接远程服务成功"); Toast.makeText(LocalService.this,"链接远程服务成功",Toast.LENGTH_LONG).show(); } @Override public void onServiceDisconnected(ComponentName name) { //对方(remoteService)被干掉了 Log.i("TAG","remoteService服务被杀"); Toast.makeText(LocalService.this,"remoteService服务被杀",Toast.LENGTH_LONG).show(); //第二次启动对方(对方会走onStart再绑定我) LocalService.this.startService(new Intent(LocalService.this,RemoteService.class)); //第二次绑定对方(我可以再次绑定对方) LocalService.this.bindService(new Intent(LocalService.this,RemoteService.class),mConn, Context.BIND_IMPORTANT); } }
}
aidl
// IMyAidlInterface.aidlpackage com.example.mypc.doubleprocess;
// Declare any non-default types here with import statements
interface IProcessService {
String getServiceName();
}
MainActivity
package com.example.mypc.doubleprocess;import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //第一次启动 this.startService(new Intent(MainActivity.this,LocalService.class)); //第一次启动 this.startService(new Intent(MainActivity.this,RemoteService.class)); }
}
Mainifest
相关文章推荐
- 两个服务的方式实现双进程守护
- Android 通过JNI实现守护进程,使Service服务不被杀死
- Hadoop 1.x 守护进程服务三种启动停止方式
- Android应对进程被杀死--Service(四) 通过JNI实现守护进程,使Service服务不被杀死
- PHP守护进程的两种常见实现方式详解
- Android 通过JNI实现守护进程,使Service服务不被杀死
- 安卓开发之使用双进程守护和进程提权来实现服务进程保活
- supervisord实现守护进程,保证后台常驻进程服务的高可用
- APP中一种在Java层实现的简单守护进程方式
- Android 通过JNI实现守护进程,使Service服务不被杀死
- Android 通过JNI实现守护进程,使得Service服务不被杀死
- Android 通过JNI实现守护进程,使Service服务不被杀死
- Android 通过JNI实现守护进程,使得Service服务不被杀死
- windows下通过bat脚本和计划任务实现设置某一服务的守护进程
- Android 通过JNI实现守护进程,使Service服务不被杀死
- Android 通过JNI实现守护进程,保证Service服务不被杀死
- Android 通过JNI实现守护进程,使得Service服务不被杀死
- Android 守护进程的实现方式
- Android 通过JNI实现守护进程,使Service服务不被杀死
- python实现将程序以守护进程的方式运行