Restart service after force stop

耗尽温柔 提交于 2019-11-28 00:05:39
Ronak Mehta

Updated :

Restart service after force stop

Answer : Sorry you can't restart it until the user manually launches application.

Assuming that your service is running as part of the process and if the user force-stops your process, you are prevented from ever running the service again until the user manually launches you.This is especially valid from 3.0 and above version ( check for yours). It also seems logical when you think that there is an app which keeps a service started all the time and is annoying the user in some way. So when the user orders a hit ( :) force-stops) on the app, it should not restart the service to continue bugging the user.

For instance, Imagine what would happen if you could create apps which just ate at your processor time by holding a wake lock, and you couldn't kill them. This would be horrible and a huge security disaster.

So, you will not be able to restart your service by any means until the user launches one of your activities.

Also you cannot disable the force-stop button AFAIK. You should take the viewpoint that nothing on the device is yours to control besides your app and (to a limited extent) the resources to which you're granted access.

Finnally, even the gtalk app will bend to your will if you desire to force stop. It will start only when you use Gtalk or other apps which use the gtalk service such as PUSH Gmail ( for phones where gtalk isnt a part of firmware).

Reference Link

Solution :

https://stackoverflow.com/a/11238779/1218762

I solved this problem by scheduling AlarmManager, to run my service again:

    public void onDestroy() {
        AlarmManager alarmMgr = (AlarmManager)this.getSystemService(this.ALARM_SERVICE);
        Intent i = new Intent(this, MyService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, i, 0);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10, pendingIntent);
    }

Create your own Exception Handler & whenever app crashes start service ..

Your Exception Handler Class

*public class MyHandler implements UncaughtExceptionHandler {
    private Context m_context;


    public static void attach(Context context) {
        Thread.setDefaultUncaughtExceptionHandler(
            new MyHandler(context)
        );
    }

    ///////////////////////////////////////////// implementation

    private MyHandler(Context context) {
        m_context=context;
    }

    public void uncaughtException(Thread thread,Throwable exception) {
    /*  StringWriter stackTrace=new StringWriter();
        exception.printStackTrace(new PrintWriter(stackTrace));*/
        System.out.println("ERROR IS "+(exception));


        Intent intent=new Intent("com.sample.service.serviceClass");  
            m_context.startService(intent);

        // from RuntimeInit.crash()
        Process.killProcess(Process.myPid());
        System.exit(10);
    }
}*

Attach In Your Activity

public class MyActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        MyHandler.attach(this);
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!