如何使service不被kill或者被kill后马上重启。
偶自己最近编了一个短信监听器,记录短信内容到SD卡上,开机自动启动已经实现了,我还想让它不被kill,或者被kill自动重启。本来是用receiver接收系统的TIME_TICK,一分钟判断service是否运行,如果否则重启。但后来发现用应用程序里管理正在运行的服务,触摸结束进程,会把整个软件连带broadcastReceiver一起结束,重启自然也无法实现。网上的一些方法我也试了,很多不可行或者太麻烦。各路大神有什么好办法?(ps:不需要root权限)

解决方案 »

  1.   

    broadcastReceiver 和你的 service 在同一个进程内。杀死你的service 你的receiver必然死掉。
    你可以跨进程通信,使用aidl。这样receiver 在一个进程里,service在一个进程里。
    这样即使杀死service,你的receiver还存在,这样它可以重新启动service.
      

  2.   

    http://topic.csdn.net/u/20111202/11/847ff783-0c8d-4097-813d-ec71d7eb6e2e.html
      

  3.   

    虽然没用过aidl,但我去试试看,谢谢
      

  4.   

    我是android2.1版的,找不到Framework目录,不过还是谢谢
      

  5.   

    自带的系统程序都是直接放在system\app里的,不root放不进去
      

  6.   


    应用层的解决方案不看framework5楼 + 17楼 = 应用层解决方案
      

  7.   

    http://developer.android.com/guide/topics/fundamentals/services.htmlRunning a Service in the ForegroundA foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.For example, a music player that plays music from a service should be set to run in the foreground, because the user is explicitly aware of its operation. The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player.
      

  8.   

    做个监听,在disconnect函数中调用重新绑定或者start
      

  9.   

    直接把进程砍掉了,没办法再启动了吧。还不能root
      

  10.   

    按你的操作步骤来停止service,该service的onDestroy()会触发,你直接在这个方法里面再启动service就可以了。我其实也遇到类似的问题,更悲剧,就是用手机管家来杀进程、清缓存,结果我感觉service重新启动的速度很慢,一直在找一种快速重新启动的方法(一分钟之内,状态栏的时间改变时,service需要启动成功)没找到。