android 使用Service进行双进程守护,防止进程被杀
来源:互联网 发布:mac查看目录结构 编辑:程序博客网 时间:2024/05/14 22:36
以下所用的方法只适用于防止app太久在后台太久而被系统杀死,在系统的一键清除功能下进程还是会被杀死的。
若要防止进程被系统的一键清除功能杀死,要设置允许当前app自启动。如何设置请看 跳转自启动管理页
定义MyService
public class MyService extends Service { String msg; public MyService() { msg = "Msg from MyService"; } @Override public IBinder onBind(Intent intent) { return new MyBinder(); } public class MyBinder extends Binder{ public String getMsg(){ return msg; } } @Override public 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() { @Override public 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(); } @Override public 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"; } @Override public IBinder onBind(Intent intent) { return new OtherBinder(); } public class OtherBinder extends Binder { public String getMsg(){ return msg; } } @Override public 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() { @Override public 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(); } @Override public 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
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zero.servicetest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService" android:enabled="true" /> <!--运行在“:OtherService”线程,防止两个服务一起被杀死--> <service android:name=".OtherService" android:enabled="true" android:exported="false" android:process=":OtherService"></service> </application></manifest>
开启服务
public class MainActivity extends AppCompatActivity { @Override protected 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); } }
阅读全文
0 0
- android 使用Service进行双进程守护,防止进程被杀
- Android Service不被杀死---守护进程
- Android防止进程被杀
- Android JNI 守护进程 service不被杀死
- android 进程防止被杀死
- Android应对进程被杀死--Service(四) 通过JNI实现守护进程,使Service服务不被杀死
- Android应对进程被杀死--Service(四) 通过JNI实现守护进程,使Service服务不被杀死
- android service 教你打造不被杀死的service-------service进程守护
- android使用JNI进程守护service
- service双进程守护
- 防止进程被杀
- Android 通过JNI实现守护进程,使得Service服务不被杀死
- Android 通过JNI实现守护进程,使得Service服务不被杀死
- Android 通过JNI实现守护进程,使Service服务不被杀死
- Android 通过JNI实现守护进程,使Service服务不被杀死
- Android 通过JNI实现守护进程,使Service服务不被杀死
- Android 通过JNI实现守护进程,使Service服务不被杀死
- Android 通过JNI实现守护进程,使Service服务不被杀死
- python 中 partial function 骗函数的使用介绍
- 四、答疑解惑之软考是什么(上)
- script元素属性
- Maven第四篇【私有仓库、上传jar包、引用私服jar包、上传本地项目到私服】
- 剑指offer JavaScript实现
- android 使用Service进行双进程守护,防止进程被杀
- VB基础知识之Do...Loop循环
- redis配置密码 1.通过配置文件进行配置 yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 [plain] view plain copy #requi
- Maven常见错误以及解决方案【转载】
- java反射详解
- 在JSP页面将时间戳转换成格式化日期
- Reveal :Xcode辅助界面调试工具
- linux 多进程并发服务__关于子进程回收的方法
- 图解正向代理、反向代理、透明代理