How to restart service in android to call service oncreate again

前端 未结 6 1516
慢半拍i
慢半拍i 2020-12-09 08:10

I have a service in my Android Application which runs always.Now i have a settings from my server through GCM and update these settings to my service. I put my settings in o

6条回答
  •  执念已碎
    2020-12-09 08:41

    or you can use a delayed handler to start the service. The handler will need to be declared static in a singleton, so its reference is not killed while restarting:

    serviceRestartHandler = new Handler ();
    serviceRestartHandler.postDelayed (new Runnable () {
                @Override
                public void run() {
    
                    startService (new Intent (mContext, YourWonderfulService.class)
                            .putExtra (flagName, true));
                    serviceRestartHandler.removeCallbacksAndMessages (null);
                }
            }, 1000);
            stopSelf ();
    

提交回复
热议问题