42

单击按钮时,我想使用方法启动服务,startService(new Intent(currentActivity.this,MyService.class))但是如果服务正在运行,我不想调用此方法以避免运行已经在运行的服务。这是怎么可能的。我同时使用Intent服务Service项目并希望对两者应用相同的条件。

4

5 回答 5

88

一个服务只会运行一次,所以你可以调用startService(Intent)多次。

您将在服务中收到一个onStartCommand()。所以记住这一点。

来源:请注意,多次调用Context.startService()不嵌套(尽管它们确实会导致多次相应的调用onStartCommand()),因此无论启动多少次服务都将停止一次Context.stopService()或被stopSelf()调用;但是,服务可以使用它们的stopSelf(int)方法来确保在处理启动的意图之前不会停止服务。

在: http: //developer.android.com/reference/android/app/Service.html主题:服务生命周期

于 2012-05-24T14:28:48.667 回答
23

使用startService(). 启动服务将调用onStartCommand()如果服务尚未启动,它将调用onCreate()。初始化您的变量和/或在onCreate().

于 2013-07-03T15:09:25.170 回答
7

绑定你的服务;开始通话时:

Intent bindIntent = new Intent(this,ServiceTask.class);
    startService(bindIntent);
    bindService(bindIntent,mConnection,0);

然后要检查您的服务是否正常工作,请使用以下方法:

    public static boolean isServiceRunning(String serviceClassName){ 
             final ActivityManager activityManager = (ActivityManager)Application.getContext().getSystemService(Context.ACTIVITY_SERVICE);     
             final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);      
           for (RunningServiceInfo runningServiceInfo : services) {         
             if (runningServiceInfo.service.getClassName().equals(serviceClassName)){             
                return true;         
             }     
           }     
         return false; 
        }
于 2012-05-24T14:31:26.653 回答
3

每当我们从任何 Activity 启动任何服务时,Android 系统都会调用该服务的 onStartCommand() 方法,如果该服务尚未运行,则系统首先调用 onCreate(),然后再调用 onStartCommand()。

所以意思是说 android 服务在其生命周期中只启动一次并保持运行直到停止。如果任何其他客户端想要再次启动它,那么只有 onStartCommand() 方法将一直被调用。

于 2017-11-15T06:07:15.013 回答
0

因此,为了避免一次又一次地重新启动任务,您可以使用布尔值,即任务已经启动或正在进行。将方法放在 oncreate 和 onstartCommand 中,并使用布尔值进行检查:

 boolean isTimerTaskRunning = false;
private boolean isServiceKeepRunning(){
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        return settings.getBoolean("silentModeKeepRunning", true);
    }

@Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: Called");
        Log.i(TAG, "onCreate: keepRunning "+isServiceKeepRunning());
        if(!isTimerTaskRunning) {
            startTimerTask();
            isTimerTaskRunning = true;
        }
        //startForeground(REQUEST_CODE /* ID of notification */, notificationbuilder().build());

    }

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        localData = new LocalData(this);
      //  return super.onStartCommand(intent, flags, startId);
        Log.i(TAG, "onStartCommand: Called");
        Log.i(TAG, "onStartCommand: keepRunning "+isServiceKeepRunning());

        Toast.makeText(this, "This is The Mode For Silent. ", Toast.LENGTH_LONG).show();

        if(!isTimerTaskRunning) {
            Log.i(TAG, "TimerTask was not Running - started from onStartCommand");
           startTimerTask();
           isTimerTaskRunning = true;

        }else {
            Log.i(TAG, "TimerTask was already Running - checked from onStartCommand");

        }
        //return START_REDELIVER_INTENT;
        startForeground(REQUEST_CODE /* ID of notification */, notificationbuilder().build());

        return  START_STICKY;
}
于 2020-12-29T15:35:53.747 回答