静态注册broadcastreceiver(就是说注册到manifest文件里),在onReceive方法里启动service即可。

解决方案 »

  1.   

    静态注册广播,如1楼说的,或者注册一个service,来到启动广播,判断是否活着,死了之后就再用service启动就可以了,因为service是杀不死的
      

  2.   

    http://www.cnblogs.com/Tiny339/archive/2013/01/21/android_5.html看看有帮助嘛
      

  3.   

    看看Android的广播机制就明白了
    http://blog.csdn.net/weihan1314/article/details/7964748
      

  4.   


    静态广播,如果进程挂了,一样会不到,我测试过。比如在设置里强行停止,就是杀掉了进程,静态广播收不到的。
    另外service也不是杀不死的啊,系统好像也会回收servcie。
      

  5.   

    1、当我用系统自带的强制停止后,demo收不到广播正常吗?Settings调用 ActivityManager.forceStopPackage()来关掉你的demo,这是个隐藏API说明是会关掉所有与包相关的东西,按照这个逻辑BroadcastReceiver估计也算
        /**
         * Have the system perform a force stop of everything associated with
         * the given application package.  All processes that share its uid
         * will be killed, all services it has running stopped, all activities
         * removed, etc.  In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED}
         * broadcast will be sent, so that any of its registered alarms can
         * be stopped, notifications removed, etc.
         * 
         * <p>You must hold the permission
         * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to
         * call this method.
         * 
         * @param packageName The name of the package to be stopped.
         * 
         * @hide This is not available to third party applications due to
         * it allowing them to break other applications by stopping their
         * services, removing their alarms, etc.
         */
    2、强制停止后,我再启动demo,为什么进程没起来呢?是不是所有运行的进程都可以在手机的设置里查看?这个估计你要检查下,应该是能再运行起来的补充一个小问题:很多人说service一般不会被回收,但我发现我的servcie经常被回收,而且我的手机是魅族MX2,配置比较高的,资源不足应该不会吧?提高你的service优先级,减少被回收几率  
    public final void startForeground (int id, Notification notification)Make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state. By default services are background, meaning that if the system needs to kill them to reclaim more memory (such as to display a large page in a web browser), they can be killed without too much harm. You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing.
      

  6.   


    多谢大牛。但第2条,我启动demo,进程却没起来,这个应该跟我没有关系啊,这个是系统的事。