Android编程防止进程被第三方软件杀死的方法
本文实例讲述了Android编程防止进程被第三方软件杀死的方法。分享给大家供大家参考,具体如下:
项目测试的时候发现,按home键回到桌面,再用360清理内存,软件被结束,再次进入的时候报错,看了下log,以为是有的地方没有控制好,但是又不知道360结束的是什么(这个现在还没弄明白)。使用小米系统的进程管理优化内存就不报错。
后来想到用Service防止软件被kill掉,查了下资料,发现google管方就有,ForegroundService前台服务,让服务一直以前台任务的方式运行,可以在service的oncreate来实现前台服务,通过这个方法必须发送一个通知栏,让用户知道服务在运行。
Notificationnotification=newNotification(R.drawable.icon,"服务开启",System.currentTimeMillis()); notification.flags|=Notification.FLAG_NO_CLEAR; notification.flags=Notification.FLAG_ONGOING_EVENT; IntentnotificationIntent=newIntent(this,MainActivity.class); PendingIntentpendingIntent=PendingIntent.getActivity(this,0,notificationIntent,0); notification.setLatestEventInfo(this,"service","防止服务被任务管理器所杀",pendingIntent); startForeground(ONGOING_NOTIFICATION,notification);
这样就能保持service运行,可是通知栏不能清除,一清除就会被kill。
后来一次做自定义Notification的时候,通知栏没有显示通知,查看后发现service也没被kill。所以就进一步去研究了下最后发现只用两行代码就能保持服务不会被kill,并且不会有通知栏通知代码如下:
Notificationnotification=newNotification(); startForeground(1,notification);
完整代码如下:
publicclassTestServiceextendsService{ privatestaticfinalClass[]mStartForegroundSignature=newClass[]{ int.class,Notification.class}; privatestaticfinalClass[]mStopForegroundSignature=newClass[]{boolean.class}; privateNotificationManagermNM; privateMethodmStartForeground; privateMethodmStopForeground; privateObject[]mStartForegroundArgs=newObject[2]; privateObject[]mStopForegroundArgs=newObject[1]; @Override publicIBinderonBind(Intentintent){ returnnull; } @Override publicvoidonCreate(){ super.onCreate(); mNM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); try{ mStartForeground=TestService.class.getMethod("startForeground", mStartForegroundSignature); mStopForeground=TestService.class.getMethod("stopForeground", mStopForegroundSignature); }catch(NoSuchMethodExceptione){ mStartForeground=mStopForeground=null; } //我们并不需要为notification.flags设置FLAG_ONGOING_EVENT,因为 //前台服务的notification.flags总是默认包含了那个标志位 Notificationnotification=newNotification(); //注意使用startForeground,id为0将不会显示notification startForegroundCompat(1,notification); } @Override publicvoidonDestroy(){ super.onDestroy(); stopForegroundCompat(1); } //以兼容性方式开始前台服务 privatevoidstartForegroundCompat(intid,Notificationn){ if(mStartForeground!=null){ mStartForegroundArgs[0]=id; mStartForegroundArgs[1]=n; try{ mStartForeground.invoke(this,mStartForegroundArgs); }catch(IllegalArgumentExceptione){ e.printStackTrace(); }catch(IllegalAccessExceptione){ e.printStackTrace(); }catch(InvocationTargetExceptione){ e.printStackTrace(); } return; } mNM.notify(id,n); } //以兼容性方式停止前台服务 privatevoidstopForegroundCompat(intid){ if(mStopForeground!=null){ mStopForegroundArgs[0]=Boolean.TRUE; try{ mStopForeground.invoke(this,mStopForegroundArgs); }catch(IllegalArgumentExceptione){ e.printStackTrace(); }catch(IllegalAccessExceptione){ e.printStackTrace(); }catch(InvocationTargetExceptione){ e.printStackTrace(); } return; } //在setForeground之前调用cancel,因为我们有可能在取消前台服务之后 //的那一瞬间被kill掉。这个时候notification便永远不会从通知一栏移除 mNM.cancel(id); } }
经测试,360手机助手,腾讯手机管家都不能kill这个service,但是手动结束后,再次打开发现音频还在播放(跟音频有关的客户端),感觉有点小别扭
希望本文所述对大家Android程序设计有所帮助。