android 使用Service进行双进程守护,防止进程被杀
以下所用的方法只适用于防止app太久在后台太久而被系统杀死,在系统的一键清除功能下进程还是会被杀死的。
若要防止进程被系统的一键清除功能杀死,要设置允许当前app自启动。如何设置请看 跳转自启动管理页
定义MyService
public class MyService extends Service {String msg;public MyService() {msg = "Msg from MyService";}@Overridepublic IBinder onBind(Intent intent) {return new MyBinder();}public class MyBinder extends Binder{public String getMsg(){return msg;}}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Intent service = new Intent(this, OtherService.class);startService(service);bindService(service, sc, Context.BIND_IMPORTANT);NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());//设置为0,通知栏不可见。设置为1,通知栏可见startForeground(0, builder.build());//返回此参数可以在Service因系统资源紧张被杀死的时候尝试重启return START_STICKY;}private ServiceConnection sc = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//由于配置之后OtherService不在同一线程,无法进行交互
// OtherService.OtherBinder otherBinder = (OtherService.OtherBinder) service;
// Toast.makeText(MyService.this, otherBinder.getMsg(), Toast.LENGTH_SHORT).show();Toast.makeText(getApplicationContext(),"绑定MyService",Toast.LENGTH_SHORT).show();}@Overridepublic void onServiceDisconnected(ComponentName name) {// 连接出现了异常断开了,OtherService被杀掉了Toast.makeText(getApplicationContext(),"OtherService挂了",Toast.LENGTH_SHORT).show();Intent service = new Intent(MyService.this, OtherService.class);startService(service);bindService(service, sc, Context.BIND_IMPORTANT);}};
} 定义OtherService
public class OtherService extends Service {String msg;public OtherService() {msg = "Msg from OtherService";}@Overridepublic IBinder onBind(Intent intent) {return new OtherBinder();}public class OtherBinder extends Binder {public String getMsg(){return msg;}}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Intent service = new Intent(this, MyService.class);startService(service);bindService(service, sc, Context.BIND_IMPORTANT);return START_STICKY;}private ServiceConnection sc = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {
// MyService.MyBinder myBinder = (MyService.MyBinder) service;
// Toast.makeText(OtherService.this,myBinder.getMsg(),Toast.LENGTH_SHORT).show();Toast.makeText(getApplicationContext(),"绑定OtherService",Toast.LENGTH_SHORT).show();}@Overridepublic void onServiceDisconnected(ComponentName name) {// 连接出现了异常断开了,MyService被杀掉了Toast.makeText(getApplicationContext(),"MyService挂了",Toast.LENGTH_SHORT).show();Intent service = new Intent(OtherService.this, MyService.class);startService(service);bindService(service, sc, Context.BIND_IMPORTANT);}};
}
配置manifest
开启服务
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent intent = new Intent(this, MyService.class);startService(intent);Intent intent2 = new Intent(this, OtherService.class);startService(intent2);}}
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent intent = new Intent(this, MyService.class);startService(intent);Intent intent2 = new Intent(this, OtherService.class);startService(intent2);}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
