我工作的家长控制/成人内容过滤应用程序。这个程序连续监视对孩子的手机的通话和短信息,并记录所有的活动到服务器。对于这个我开始一个服务(MyService.java) 对BOOT_COMPLETED和服务的onCreate方法我注册一个contentobserver为callLog和短信URI(参见code段以下)。
现在的问题是,因为我要监控每个传出,呼叫进入S和短信我希望服务是时间可持续运行(而不会被停止/杀死)。此外,这种服务被只用于注册内容观察员和没有做任何其他处理(其OnstartCommand方法是假),所以之后的某个Android操作系统杀死服务。 我如何确保服务运行时间可持续,并保持contentobserver对象还活着吗?
谢谢 Srao
公共类的MyService延伸服务{ 私人CallLogObserver clLogObs = NULL; 公共无效的onCreate(){ super.onCreate(); 尝试{ clLogObs =新CallLogObserver(本); 。this.getContentResolver()registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI,真实,clLogObs); }赶上(例外前) { Log.e(CallLogData,ex.toString()); } } @覆盖 公众的IBinder onBind(意向为arg0){ // TODO自动生成方法存根 返回null; } @覆盖 公共无效的onDestroy(){ 如果(clLogObs!= NULL) { 。this.getContentResolver()unregisterContentObserver(clLogObs); } super.onDestroy(); } @覆盖 公众诠释onStartCommand(意向意图,诠释标志,诠释startId){ super.onStartCommand(意向,标志,startId); 返回Service.START_STICKY; } @覆盖 公共布尔onUnbind(意向意图){ 返回super.onUnbind(意向); }
解决方案
您可以设置为在前台运行的服务。这会给你的服务被杀害的操作系统低得多的机会。
阅读这里了解更多信息。
I am working on parental control/adult content filtering application. This app continuously monitors the calls and smses on a child's mobile and logs all the activity onto a server. For this I am starting a service (MyService.java) on BOOT_COMPLETED and in the onCreate method of the service I register a contentobserver for the callLog and sms uri ( refer to the code snippet below ) .
Now the issue is, Since I want to monitor every outgoing, incoming call s and sms I want the service to be continuosly running ( without being stopped/killed) . Moreover this Service is being just used for registering content observers and not doing any other processing(its OnstartCommand method is dummy ) , so android OS kills the service after sometime. How do I ensure that the service runs continuosly and keeps the contentobserver object alive ?
Thanks Srao
public class MyService extends Service { private CallLogObserver clLogObs = null; public void onCreate() { super.onCreate(); try{ clLogObs = new CallLogObserver(this); this.getContentResolver().registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI, true, clLogObs); }catch(Exception ex) { Log.e("CallLogData", ex.toString()); } } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onDestroy() { if( clLogObs !=null ) { this.getContentResolver().unregisterContentObserver(clLogObs); } super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); return Service.START_STICKY; } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); }
解决方案
you can set the service to run in the foreground . this will give your service a much lower chance of being killed by the OS .
read here for more information .
上一篇:背景VS意见背景、意见、VS
下一篇:UnsupportedOperationException异常同时呼吁getShared preferences()的单元测试单元测试、异常、UnsupportedOperationException