在我的代码中,我会根据条件来启动我的服务,如下所示:
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
请问我是否可以查找之前是否已经启动了相同的服务,以便不会重复启动服务?
谢谢。
在我的代码中,我会根据条件来启动我的服务,如下所示:
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
请问我是否可以查找之前是否已经启动了相同的服务,以便不会重复启动服务?
谢谢。
请看以下内容:
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;}
@bigstones: 我不知道怎么做,但我有一个服务,它可以随意创建多少次(可能是因为我在其中创建了各种可运行的对象)。