2014-02-21 77 views

回答

1

實測值的解決方案:如果服務正在運行

  1. 方法用於檢查

    private boolean isMyServiceRunning(String className) { 
    ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE); 
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { 
        if (className.equals(service.service.getClassName())) { 
         return true; 
        } 
    } 
    return false;} 
    
  2. 開始意圖ACTION_SET_WALLPAPER

    //Check if Wallpaper Service is active 
    if(isMyServiceRunning(MyWallpaperService.class.getName())){ 
        Log.d(TAG, "active"); 
    } else { 
        Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER); 
        startActivity(Intent.createChooser(intent, getActivity().getString(R.string.title_for_wallpaper_chooser))); 
    }