Android background service stop after activity failure

In the beginning, I would like to apologize for bad English.

There is my problem:

I have a service in android that works in the background when the work is done. (This service synchronizes user data with the server at specified time intervals).

public class CService extends Service { private Boolean isDestroyed; @Override public int onStartCommand (Intent intent, int flags, int startId) { if (intent != null) { new Thread(new Runnable() { //run new thread to disable flush memory for android and destroy this service @Override public void run () { this.isDestroyed = Boolean.FALSE while(!this.isDestroyed) { //loop until service isn't destroyed } } }).start(); } return Service.START_NOT_STICKY; } @Override public void onDestroy () { //THIS ISNT CALLED FROM uncaughtException IN ACTIVITY BUT from onDestroy method is this called. //when is service destroyed then onDestroy is called and loop finish this.isDestroyed = Boolean.TRUE; } } 

And it starts with an action in onCreateMethod. This operation implements Thread.UncaughtExceptionHandler and is registered in the onCreate method to catch all unexpected exceptions in the activity. When something in the action is called, the uncaughtException exception method is called and the service should stop with stopService (serviceIntent); But onDestoy is not called in the service. But when the onDestroy method in the named activity (user push button and vice versa) ends successfully, and onDestoroy in the CService is called.

  public class CActivity extends Activity implements Thread.UncaughtExceptionHandler { private Thread.UncaughtExceptionHandler defaultUEH; @Override protected void onCreate (Bundle savedInstanceState) { this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); // create intent for service Intent serviceIntent = new Intent(this, CService.class); // run service startService(serviceIntent); //set default handler when application crash Thread.setDefaultUncaughtExceptionHandler(this); super.onCreate(savedInstanceState); } @Override public void uncaughtException (Thread thread, Throwable ex) { //THIS DOESN'T WORK //when global exception in activity is throws then this method is called. Intent serviceIntent = new Intent(this, CService.class); //method to service stop is called. BUT THIS METHOD DON'T CALL onDestroy in CService stopService(serviceIntent); defaultUEH.uncaughtException(thread, ex); } @Override public void onDestroy () { //this work fine Intent serviceIntent = new Intent(this, CService.class); stopService(serviceIntent); super.onDestroy(); } } 

I need to stop background maintenance when activity fails. Bacause, when the android closes the activity and starts the previous activity on the stack (this is the login screen), the user is not logged in.

Thanks for the suggestions.

+4
source share
2 answers

try it

  Thread.currentThread().setUncaughtExceptionHandler(this); 

UPD
this is the default exception handler by default.

 private static class UncaughtHandler implements Thread.UncaughtExceptionHandler { public void uncaughtException(Thread t, Throwable e) { try { // Don't re-enter -- avoid infinite loops if crash-reporting crashes. if (mCrashing) return; mCrashing = true; if (mApplicationObject == null) { Slog.e(TAG, "*** FATAL EXCEPTION IN SYSTEM PROCESS: " + t.getName(), e); } else { Slog.e(TAG, "FATAL EXCEPTION: " + t.getName(), e); } // Bring up crash dialog, wait for it to be dismissed ActivityManagerNative.getDefault().handleApplicationCrash( mApplicationObject, new ApplicationErrorReport.CrashInfo(e)); } catch (Throwable t2) { try { Slog.e(TAG, "Error reporting crash", t2); } catch (Throwable t3) { // Even Slog.e() fails! Oh well. } } finally { // Try everything to make sure this process goes away. Process.killProcess(Process.myPid()); System.exit(10); } } } 

I believe that you are not getting an onDestroy () call because the default android exception handler just destroys the entire System.exit () process. You can try not to raise defaultUEH.uncaughtException (thread, ex); or call it later (if) your service will be destroyed.

+2
source

Dude, by the way, why are you doing things in the onCreate method? Since the docs say this method is being called from the main thread, I really wonder how it works. You have to do some heavy calculations from another thread.

http://developer.android.com/reference/android/app/Service.html

When a service component is actually created, for any of these reasons, all that the system actually does is call onCreate () and any other relevant callbacks on the main thread. The service must perform these functions using appropriate behavior, for example, creating a secondary thread in which it performs its work.

+1
source

Source: https://habr.com/ru/post/1444770/


All Articles