Android安卓进程保活(二):双进程拉活
当一个进程结束后,立刻调用启动另一个进程,这样实现互相调用,互相启动( 只有在一个进程结束时候才会启动另一个进程)
1.首先创建LocalService.java继承自Service(android.app.Service):
public class LocalService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new LocalBinder();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
/*第一个参数Intent
第二个参数ServiceConnection*/
/**
* 第三个参数介绍:
* Flag for {@link #bindService}: automatically create the service as long
* as the binding exists. Note that while this will create the service,
* its {@link android.app.Service#onStartCommand}
* method will still only be called due to an
* explicit call to {@link #startService}. Even without that, though,
* this still provides you with access to the service object while the
* service is created.
*
* <p>Note that prior to {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH},
* not supplying this flag would also impact how important the system
* consider's the target service's process to be. When set, the only way
* for it to be raised was by binding from a service in which case it will
* only be important when that activity is in the foreground. Now to
* achieve this behavior you must explicitly supply the new flag
* {@link #BIND_ADJUST_WITH_ACTIVITY}. For compatibility, old applications
* that don't specify {@link #BIND_AUTO_CREATE} will automatically have
* the flags {@link #BIND_WAIVE_PRIORITY} and
* {@link #BIND_ADJUST_WITH_ACTIVITY} set for them in order to achieve
* the same result.
*/
bindService(new Intent(this,RemoteService.class),connection,Context.BIND_AUTO_CREATE);
return super.onStartCommand(intent, flags, startId);
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//绑定成功
}
@Override
public void onServiceDisconnected(ComponentName name) {
//当RemoteService所处进程被干掉就重新启动
startService(new Intent(LocalService.this,RemoteService.class));
bindService(new Intent(LocalService.this,RemoteService.class),connection,Context.BIND_IMPORTANT);
}
};
private class LocalBinder extends Binder {
}
}
对LocalService在清单文件中进行注册
<service android:name=".LocalService" />
2.创建RemoteService.java继承自Service(android.app.Service):↓
public class RemoteService extends Service {
public RemoteService() {
}
@Override
public IBinder onBind(Intent intent) {
return new RemoteBinder();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
/*第一个参数Intent
第二个参数ServiceConnection
第三个参数介绍:
/**
* Flag for {@link #bindService}: automatically create the service as long
* as the binding exists. Note that while this will create the service,
* its {@link android.app.Service#onStartCommand}
* method will still only be called due to an
* explicit call to {@link #startService}. Even without that, though,
* this still provides you with access to the service object while the
* service is created.
*
* <p>Note that prior to {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH},
* not supplying this flag would also impact how important the system
* consider's the target service's process to be. When set, the only way
* for it to be raised was by binding from a service in which case it will
* only be important when that activity is in the foreground. Now to
* achieve this behavior you must explicitly supply the new flag
* {@link #BIND_ADJUST_WITH_ACTIVITY}. For compatibility, old applications
* that don't specify {@link #BIND_AUTO_CREATE} will automatically have
* the flags {@link #BIND_WAIVE_PRIORITY} and
* {@link #BIND_ADJUST_WITH_ACTIVITY} set for them in order to achieve
* the same result.
*/
bindService(new Intent(this,RemoteService.class),connection,Context.BIND_AUTO_CREATE);
return super.onStartCommand(intent, flags, startId);
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//绑定成功
}
@Override
public void onServiceDisconnected(ComponentName name) {
//当RemoteService所处进程被干掉就重新启动
startService(new Intent(RemoteService.this,LocalService.class));
bindService(new Intent(RemoteService.this,LocalService.class),connection,Context.BIND_IMPORTANT);
}
};
private class RemoteBinder extends Binder{
}
}
对RemoteService 在清单文件中进行注册,再制定一个进程名字,好区分
<service
android:name=".RemoteService"
android:enabled="true"
android:exported="true"
android:process=":remote" />
<!--
android:process=":remote"
指定进程名
-->
最后在MainActivity启动其中一个服务LocalService:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//双进程拉活(Java层)
startService(new Intent(this,LocalService.class));
}
}
这样就完成了双进程拉活
原文地址:https://blog.csdn.net/qq_39431405/article/details/144426593
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!