如果已经运行android,如何防止服务再次运行
单击一个按钮,我想使用方法如果已经运行android,如何防止服务再次运行,android,service,android-service,Android,Service,Android Service,单击一个按钮,我想使用方法startService(新意图(currentActivity.this,MyService.class))启动服务但如果服务正在运行,我不想调用此方法来避免运行已经运行的服务。这是如何实现的。我在同一项目中同时使用意向服务和服务,并希望对两者应用相同的条件。一个服务只运行一次,因此您可以调用启动服务(意向)多次 您将在服务中收到一个onStartCommand()。所以请记住这一点 资料来源: 请注意,对Context.startService()的多个调用不会嵌套
startService(新意图(currentActivity.this,MyService.class))启动服务
但如果服务正在运行,我不想调用此方法来避免运行已经运行的服务。这是如何实现的。我在同一项目中同时使用意向服务和服务,并希望对两者应用相同的条件。一个服务只运行一次,因此您可以调用启动服务(意向)
多次
您将在服务中收到一个onStartCommand()
。所以请记住这一点
资料来源:
请注意,对Context.startService()
的多个调用不会嵌套(尽管它们会导致对onStartCommand()
的多个相应调用),因此无论服务启动多少次,只要调用Context.stopService()
或stopSelf()
,服务都会停止;但是,服务可以使用其stopSelf(int)
方法来确保在处理启动的意图之前不会停止服务
在:关于主题:服务生命周期绑定您的服务;开始通话时:
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;
}
public静态布尔isServiceRunning(字符串serviceClassName){
最终ActivityManager ActivityManager=(ActivityManager)Application.getContext().getSystemService(Context.ACTIVITY_服务);
最终列表服务=activityManager.getRunningServices(Integer.MAX_值);
对于(RunningServiceInfo RunningServiceInfo:services){
如果(runningServiceInfo.service.getClassName().equals(serviceClassName)){
返回true;
}
}
返回false;
}
使用startService()
。
启动服务将调用
onStartCommand()
,如果服务尚未启动,它将调用onCreate()
。初始化变量和/或在onCreate()
中启动线程每当我们从任何活动启动任何服务时,Android系统都会调用该服务的onStartCommand()方法,如果该服务尚未运行,则系统首先调用onCreate(),然后调用onStartCommand()
也就是说,android服务在其生命周期中只启动一次,并一直运行到停止。如果任何其他客户端想要再次启动它,则始终只调用onStartCommand()方法。因此,为了避免一次又一次地重新启动任务,您可以使用布尔值,即任务已经启动或正在进行。将该方法放在oncreate和onStart命令中,并使用布尔值进行检查:
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;
}
只有当我将服务绑定到活动时,此代码才起作用吗???我认为将服务绑定到活动将一直运行,直到应用程序运行。请注意,这不会一直起作用。它不考虑“正在停止的服务”。主要用于显示正在运行的服务UI。我用过这个,但它在KitKat不会一直起作用。相反,您可以始终使用startService,并使用ServiceOnCreate和onStartCommand来执行任务。这是正确的方式。为什么这里需要绑定服务?我们不能只使用已启动的服务吗?getRunningServices不受欢迎,谢谢您的快速回复。您的评论将对我有很大帮助,但如果服务正在运行,我不想再次调用这些方法。还有其他想法吗?@AtulBhardwaj:Calls to
startService()
始终触发onStartComand()
。时期因此,要么不要调用startService()
,要么使用更智能的onStartCommand()
,可以处理多次调用。这是正确的答案。忽略了onCreate()。这意味着我们只想启动一次onCreate调用服务多少次?是否有文档来源?