서비스를 Android에서 작동 시키려고합니다. 내 응용 프로그램은 지금 내 서비스를 시작할 수 있지만 서비스가 백그라운드에서 실행 중이고 내 (Main) 활동이 시작되면 서비스가 다시 시작됩니다 (onCreate()
및 onStartCommand()
명령이 호출됩니다). 내 콘솔에서활동 시작시 서비스 다시 시작
if (isServiceRunning()){
System.out.println("De serice is running");
}
else{
System.out.println("De service is niet running");
startService(new Intent(this, YourService.class));
}
...
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
Log.d("services", service.service.getClassName());
if ("com.vandervoorden.mijncijfers.YourService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
내 서비스 "드의 serice가 실행"실행 표시됩니다 : 서비스가 이미 다음과 같은 코드를 통해 실행중인 경우 내 MainActivity에서 나는 확인한다.
직접 서비스를 다시 시작하지 않는 동안 onCreate()
과 onStartCommand()
이 호출되는 이유는 무엇입니까? 이미 시작된 서비스가 두 번 시작되지 않도록하려면 어떻게해야합니까?
EDIT 1 2013년 10월 7일 오후 8시 54분 :
내 서비스 클래스 :이 동작은 당신이 서비스를 어딘가에 파괴되지 않은있는 경우에만 이상한 보이는
public class YourService extends Service
{
Alarm alarm = new Alarm();
public void onCreate()
{
super.onCreate();
System.out.println("service: onCreate");
}
public int onStartCommand(Intent intent, int flags, int startId) {
//alarm.SetAlarm(this);
System.out.println("service: onStartCommand");
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
}