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() {
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.
source share