簡體   English   中英

重新啟動我的Android服務

[英]Restarting my android service

我想完全停止服務,然后以編程方式再次運行它。 但由於它稱為OnDestroy()或stopSelf()方法,因此無法再次啟動。 有什么建議我該如何重啟我的服務?

我在我的應用程序中使用這種方式:

停止服務:

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

啟動服務:

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

isMyServiceRunning方法:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM