简体   繁体   中英

Restarting my android service

I want to stop my service completely and run it again programmatically by itself. but as it called OnDestroy() or stopSelf() methods, it can not start again. how can i restart my service, any suggestion?

I am using this way in my apps :

Stopping service :

mContext.stopService(new Intent(mContext, LocationProviderService.class));

Starting service :

if(!HelperUtils.isMyServiceRunning(LocationProviderService.class, getApplicationContext()))
  {
      startService(new Intent(this, LocationProviderService.class));
  }

isMyServiceRunning method :

public static boolean isMyServiceRunning(Class<?> serviceClass,Context mContext) {
    ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM