Restart service after force stop
Asked Answered
C

3

8

I am developing an application which is used as application locker, an app which is able to protect other installed apps by asking the user for a password on opening those apps, my app is here

The problem is that the application can be easily skipped by force close it from the android task manager, how can I overcome this?

Also what is the best way to check for a new application open, to make a service which check every one second for the app on the top of tasks or to fire alarm with alarm manager every second to make the check.

Crosstie answered 10/6, 2013 at 11:36 Comment(2)
[i have a solution but not recommended, refer this answer][1] [1]: #12672938Sketchbook
@Omar if get any solution then pls postAdvantage
B
5

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://mcmap.net/q/245656/-how-does-whatsapp-service-gets-restarted-even-if-i-force-stop-app

Broncobuster answered 10/6, 2013 at 11:39 Comment(5)
but android kill my service on selecting force stop, but if you clicked stop the service will work again.Crosstie
what if i am using AlarmManager to schedule an laram every one second and the user stopped the app, the aralm will stop also ?Crosstie
The problem is in some chinese mobiles where even when swipe closed it force stops that app. And it wont function till you open it again.Antecedents
Hey Srihari, You probably talk about Vivo and Xiaomi. I will have some research on this devices and keep this answer updated. These devices were not exist when I answered. Sorry for that.Broncobuster
What about the gmail widget app? When I force stop it and go back to the home screen it starts the service again.Williams
S
1

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);
    }
Scarabaeid answered 4/1, 2016 at 14:24 Comment(3)
ASAIK, If you have a scheduled alarm and the user forced stop for your app, the system will cancel your alarm.Crosstie
this does the trick, however I made call to super.onDestroy() after execution of the above (AlarmManager) code.Besnard
Where will this onDestroy be called? Application?Excision
R
-1

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);
}
}
Riehl answered 10/6, 2013 at 11:55 Comment(2)
Is an exception thrown when the user force stops the service?Beatrix
@msn Does this work if I attach this a Service lets say, I attach this a Service and if some one stops the service, it will restart the service. I would have attach the code to the ondestroy() however I heard that forcibly stopping the app may not call onDestroy()Parrotfish

© 2022 - 2024 — McMap. All rights reserved.