android开发之如何保证service不被杀掉(前台服务) -尊龙游戏旗舰厅官网
序言
最近项目要实现这样一个效果:运行后,要有一个service始终保持在后台运行,不管用户作出什么操作,都要保证service不被kill。参考了现今各种定制版的系统和安全厂商牛虻软件,如何能保证自己的service不被杀死呢?其实除了常规的手段,我们可以参考一下微信和360,设置-程序-正在运行,可以看到微信是同时开启了两个进程和服务:
service简介
service是在一段不定的时间运行在后台,不和用户交互应用组件。每个service必须在manifest中 通过
本地服务 local service 用于应用程序内部
它可以启动并运行,直至有人停止了它或它自己停止。在这种方式下,它以调用context.startservice()启动,而以调用context.stopservice()结束。它可以调用service.stopself() 或 service.stopselfresult()来自己停止。不论调用了多少次startservice()方法,你只需要调用一次stopservice()来停止服务。
【用于实现应用程序自己的一些耗时任务,比如查询升级信息,并不占用应用程序比如activity所属线程,而是单开线程后台执行,这样用户体验比较好】
远程服务 remote service 用于android系统内部的应用程序之间
它可以通过自己定义并暴露出来的接口进行程序操作。客户端建立一个到服务对象的连接,并通过那个连接来调用服务。连接以调用context.bindservice()方法建立,以调用 context.unbindservice()关闭。多个客户端可以绑定至同一个服务。如果服务此时还没有加载,bindservice()会先加载它。
【可被其他应用程序复用,比如天气预报服务,其他应用程序不需要再写这样的服务,调用已有的即可】
1,service的生命周期
2,service运行方式
以startservice()启动服务,系统将通过传入的intent在底层搜索相关符合intent里面信息的service。如果服务没有启动则先运行oncreate,然后运行onstartcommand (可在里面处理启动时传过来的intent和其他参数),直到明显调用stopservice或者stopself才将停止service。无论运行startservice多少次,只要调用一次stopservice或者stopself,service都会停止。使用stopself(int)方法可以保证在处理好intent后再停止。onstartcommand ,在2.0后被引入用于service的启动函数,2.0之前为public void onstart(intent intent, int startid) 。
以bindservice()方法启用服务,调用者与服务绑定在了一起,调用者一旦退出,服务也就终止。onbind()只有采用context.bindservice()方法启动服务时才会回调该方法。该方法在调用者与服务绑定时被调用,当调用者与服务已经绑定,多次调用context.bindservice()方法并不会导致该方法被多次调用。采用context.bindservice()方法启动服务时只能调用onunbind()方法解除调用者与服务解除,服务结束时会调用ondestroy()方法。
3,拥有service的进程具有较高的优先级
官方文档告诉我们,android系统会尽量保持拥有service的进程运行,只要在该service已经被启动(start)或者客户端连接(bindservice)到它。当内存不足时,需要保持,拥有service的进程具有较高的优先级。
1. 如果service正在调用oncreate,onstartcommand或者ondestory方法,那么用于当前service的进程则变为前台进程以避免被killed。
2. 如果当前service已经被启动(start),拥有它的进程则比那些用户可见的进程优先级低一些,但是比那些不可见的进程更重要,这就意味着service一般不会被killed.
3. 如果客户端已经连接到service (bindservice),那么拥有service的进程则拥有最高的优先级,可以认为service是可见的。
4. 如果service可以使用startforeground(int, notification)方法来将service设置为前台状态,那么系统就认为是对用户可见的,并不会在内存不足时killed。
5. 如果有其他的应用组件作为service,activity等运行在相同的进程中,那么将会增加该进程的重要性。
实现前台:
service如果要防止尽可能不被系统杀掉,需要设置为在前台运行。
1.mainactivity.java
1 package com.example.helloteacher; 2 3 import com.example.teacherservice.helloteacherservice; 4 import com.example.teacherservice.helloteacherservice2; 5 6 import android.app.activity; 7 import android.content.intent; 8 import android.os.bundle; 9 import android.util.log; 10 import android.view.view; 11 import android.widget.button; 12 import android.widget.textview; 13 14 public class mainactivity extends activity { 15 private textview fuction; 16 private button start; 17 @override 18 protected void oncreate(bundle savedinstancestate) { 19 super.oncreate(savedinstancestate); 20 setcontentview(r.layout.activity_main); 21 //初始化控件 22 init(); 23 //创建进程 24 inprocess(); 25 } 26 ///初始化控件函数 27 private void init() { 28 start=(button)findviewbyid(r.id.button1); 29 fuction=(textview)findviewbyid(r.id.tv_bottom_funtion); 30 start.setonclicklistener(new startonclicklistener()); 31 fuction.setonclicklistener(new fuctiononclicklistener()); 32 } 33 //功能按钮监听实现函数 34 private final class fuctiononclicklistener implements view.onclicklistener{ 35 36 @override 37 public void onclick(view v) { 38 // todo auto-generated method stub 39 intent intent = new intent(mainactivity.this, fuctionactivity.class); 40 startactivity(intent); 41 } 42 } 43 //进程函数 44 private void inprocess(){ 45 intent intent =new intent(mainactivity.this, helloteacherservice.class); 46 startservice(intent); 47 intent intent2 =new intent(mainactivity.this, helloteacherservice2.class); 48 startservice(intent2); 49 } 50 private final class startonclicklistener implements view.onclicklistener{ 51 52 @override 53 public void onclick(view v) { 54 55 } 56 } 57 58 59 }2.helloteacherservice.java
1 package com.example.teacherservice; 2 3 import com.example.receiver.alarmreceiver; 4 import com.example.helloteacher.r; 5 6 import android.app.alarmmanager; 7 import android.app.notification; 8 import android.app.pendingintent; 9 import android.app.service; 10 import android.content.intent; 11 import android.os.ibinder; 12 import android.os.systemclock; 13 import android.util.log; 14 15 public class helloteacherservice extends service { 16 helloteacherservice2 hs2; 17 private string tag="helloteacherservice"; 18 @override 19 public void oncreate() { 20 super.oncreate(); 21 log.i(tag, "-->>oncreate"); 22 } 23 24 @override 25 public int onstartcommand(intent intent, int flags, int startid) { 26 log.i(tag, "-->>onstartcommand-->>" startid); 27 flags = start_sticky; 28 29 //启用前台服务,主要是startforeground() 30 notification notification = new notification(r.drawable.ic_launcher, "用电脑时间过长了!白痴!" 31 , system.currenttimemillis()); 32 notification.setlatesteventinfo(this, "快去休息!!!", 33 "一定保护眼睛,不然遗传给孩子,老婆跟别人跑啊。", null); 34 //设置通知默认效果 35 notification.flags = notification.flag_show_lights; 36 startforeground(1, notification); 37 38 alarmmanager manager = (alarmmanager) getsystemservice(alarm_service); 39 //读者可以修改此处的minutes从而改变提醒间隔时间 40 //此处是设置每隔55分钟启动一次 41 //这是55分钟的毫秒数 42 int minutes = 55 * 60 * 1000; 43 //systemclock.elapsedrealtime()表示1970年1月1日0点至今所经历的时间 44 long triggerattime = systemclock.elapsedrealtime() minutes; 45 //此处设置开启alarmreceiver这个service 46 intent i = new intent(this, alarmreceiver.class); 47 pendingintent pi = pendingintent.getbroadcast(this, 0, i, 0); 48 //elapsed_realtime_wakeup表示让定时任务的出发时间从系统开机算起,并且会唤醒cpu。 49 manager.set(alarmmanager.elapsed_realtime_wakeup, triggerattime, pi); 50 51 return super.onstartcommand(intent, flags, startid); 52 } 53 //监听服务2实现函数 54 55 56 @override 57 public ibinder onbind(intent arg0) { 58 log.i(tag, "-->>onbind"); 59 // todo auto-generated method stub 60 return null; 61 } 62 @override 63 public void ondestroy() { 64 log.i(tag, "-->>ondestroy"); 65 super.ondestroy(); 66 67 } 68 }3.bootreceiver.java
1 package com.example.receiver; 2 3 import android.app.alarmmanager; 4 import android.app.pendingintent; 5 import android.content.broadcastreceiver; 6 import android.content.context; 7 import android.content.intent; 8 import android.os.systemclock; 9 10 public class bootreceiver extends broadcastreceiver { 11 12 /*要接收的intent源*/ 13 static final string action = "android.intent.action.boot_completed"; 14 15 public void onreceive(context context, intent mintent) 16 { 17 if (intent.action_boot_completed.equals(mintent.getaction())) { 18 // 启动完成 19 intent intent = new intent(context, alarmreceiver.class); 20 intent.setaction("arui.alarm.action"); 21 pendingintent sender = pendingintent.getbroadcast(context, 0, 22 intent, 0); 23 long firstime = systemclock.elapsedrealtime(); 24 alarmmanager am = (alarmmanager) context 25 .getsystemservice(context.alarm_service); 26 27 // 10秒一个周期,不停的发送广播 28 am.setrepeating(alarmmanager.elapsed_realtime_wakeup, firstime, 29 10 * 1000, sender); 30 } 31 } 32 33 34 }4.alarmreceiver.java
1 package com.example.receiver; 2 3 import com.example.teacherservice.helloteacherservice; 4 5 import android.content.broadcastreceiver; 6 import android.content.context; 7 import android.content.intent; 8 9 public class alarmreceiver extends broadcastreceiver { 10 11 @override 12 public void onreceive(context context, intent intent) { 13 // todo auto-generated method stub 14 if (intent.getaction().equals("arui.alarm.action")) { 15 intent i = new intent(); 16 i.setclass(context, helloteacherservice.class); 17 // 启动service 18 // 多次调用startservice并不会启动多个service 而是会多次调用onstart 19 context.startservice(i); 20 } 21 } 22 23 }5.配置文件
androidmanifest.xml
下面是添加在配置文件中的部分代码:
1 <activity android:name="com.example.helloteacher.fuctionactivity" /> 2 <service android:name="com.example.teacherservice.helloteacherservice" android:process=":helloteacherservice"/> 3 <service android:name="com.example.teacherservice.helloteacherservice2"/> 4 <uses-permission android:name="android.permission.receive_boot_completed">uses-permission> 5 <receiver android:name="com.example.receiver.bootreceiver" > 6 <intent-filter> 7 <action android:name="android.intent.action.boot_completed" /> 8 intent-filter> 9 receiver> 10 <receiver android:name="com.example.receiver.alarmreceiver" > 11 <intent-filter> 12 <action android:name="arui.alarm.action" /> 13 intent-filter> 14 receiver>通过以上的配置,就可以实现service的前台运行!下面附上效果图:
转载于:https://www.cnblogs.com/moutou/p/5607423.html
总结
以上是尊龙游戏旗舰厅官网为你收集整理的android开发之如何保证service不被杀掉(前台服务)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 网路神警上网行为管理系统 v3.4.5官
- 下一篇: redis学习笔记(4)-list