Service Automatic Called on Destroying Activity

后端 未结 4 927
天涯浪人
天涯浪人 2020-12-16 15:17

I am stuck with the problem of Activity + Service in that I have following number of Activities and Services.

Activities:

相关标签:
4条回答
  • 2020-12-16 15:50

    (1)For Your Dialog:

    The solution is to call dismiss() on the Dialog you created before exiting the Activity, e.g. in onDestroy(). All Windows & Dialog should be closed before leaving an Activity.

    (2)For Your service autostart:

    you have to look at the value the service returns from its onStartCommand method. The default value is START_STICKY which will restart the service after it is destroyed. Take a look at the onStartCommand documentation for more details:

    0 讨论(0)
  • 2020-12-16 15:59

    Services got killed when application got killed (add logs in service onStartCommand() and onDestroy() function and try clearing app from recent list and you will see onDestroy() is called. Android will re-start service if you have returned START_STICKY intent in onStartCommand()).

    There are two approaches to fix your problem.

    1. Either make your two services as foreground service.

    2. Instead of using CheckAutoSyncReceivingOrder and CheckAutoSyncSendingOrder to schedule start of another services, you should use AlarmManager to schedule your task.

    0 讨论(0)
  • 2020-12-16 16:10

    You need to either run your service in the foreground so when the activity is destroyed so will the service or use a bound service and manage the binding with the activity lifecycle, so it is not continually restarted when the activity is destroyed.

    From this android docs tutorial Bound Services

    You need to do this for each service.

    public class CheckAutoSyncReceivingOrder extends Service {
        // Binder given to clients
        private final IBinder mBinder = new LocalBinder();
    
        public class LocalBinder extends Binder {
            CheckAutoSyncReceivingOrder getService() {
            return CheckAutoSyncReceivingOrder.this;
        }
    }
    
        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }   
    

    From your activity that creates and calls the service, that when it is destroyed you want your service destroyed.

    public class BindingActivity extends Activity {
        CheckAutoSyncReceivingOr mService;
        boolean mBound = false;
    
    
        @Override
        protected void onStart() {
            super.onStart();
            // Bind to CheckAutoSyncReceivingOr
            Intent intent = new Intent(this, CheckAutoSyncReceivingOr.class);
            bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            // Unbind from the service
            if (mBound) {
                unbindService(mConnection);
                mBound = false;
            }
        }
    
        /** Defines callbacks for service binding, passed to bindService() */
        private ServiceConnection mConnection = new ServiceConnection() {
    
            @Override
            public void onServiceConnected(ComponentName className,
                IBinder service) {
                // We've bound to CheckAutoSyncReceivingOr, cast the IBinder and get CheckAutoSyncReceivingOr instance
                LocalBinder binder = (LocalBinder) service;
                mService = binder.getService();
                mBound = true;
            }
    
            @Override
            public void onServiceDisconnected(ComponentName arg0) {
                mBound = false;
            }
        };
    }   
    

    And manage the service lifecycle. Restart the same service with your timer, do not create a new service.

    public class ExampleService extends Service {
        int mStartMode;       // indicates how to behave if the service is killed
        IBinder mBinder;      // interface for clients that bind
        boolean mAllowRebind; // indicates whether onRebind should be used
    
        @Override
        public void onCreate() {
            // The service is being created
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // The service is starting, due to a call to startService()
            return mStartMode;
        }
        @Override
        public IBinder onBind(Intent intent) {
            // A client is binding to the service with bindService()
            return mBinder;
        }
        @Override
        public boolean onUnbind(Intent intent) {
            // All clients have unbound with unbindService()
            return mAllowRebind;
        }
        @Override
        public void onRebind(Intent intent) {
            // A client is binding to the service with bindService(),
            // after onUnbind() has already been called
        }
        @Override
        public void onDestroy() {
            // The service is no longer used and is being destroyed
        }
    }
    

    Note START_NOT_STICKY will only prevent the service from restarting if the device is low on memory.

    Be mindful that you where you are starting services, just start it once and allow the service to maintain it's own lifecycle until you destroy it with your activity.

    This is in reply to your original unedited question, when the app was mysteriously crashing:

    You need to destroy the dialog before the context window the dialog is attached to. That will cause a problem. So this is where program flow and the order of closing and cleaning up resources is important. They, frequently have to be destroyed in the reverse order they were created if they are dependent upon parent windows (which is often in the form of a particular activity).

    It's difficult to trace your code, so this is a generic answer.

    Make use of onPause and onDestroy in your activities.

    In all your activities, manage any resources you have created within that activity and with a null check, close them down. Like you have in your service class. If you want to override the parent onDestroy, place your custom code before super.onDestroy.

    protected void onDestroy() {
    
        if(timer != null)
            timer.cancel();
    
        Log.d(TAG, "Stopping Sending...");
    
        super.onDestroy();
    }
    
    0 讨论(0)
  • 2020-12-16 16:16

    If the process that runs your service gets killed, the Android system will restart it automatically it is default behavior.

    This behavior is defined by the return value of onStartCommand() in your Service implementation. The constant START_NOT_STICKY tells Android not to restart the service if it s running while the process is "killed".

    You need to Override method onStartCommand() in your service class and move all your code from onStart() method to onStartCommand() method.

    According to the Android Documentation:

    For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them

    onStart() method calls each time when service is restarted but onStartCommand() method will not called if you return START_NON_STICKY.

    Don't use onStart() anymore, it's deprecated.

    I hope it helps you.

    0 讨论(0)
提交回复
热议问题