2011-09-30 10 views
2

알람을 취소하고 서비스를 시작하는 데 문제가 있습니다.alarmManager로 시작한 서비스를 중지 할 수 없습니다.

은 내가 활동에 알람을 설정

public class AlarmStarter extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Intent locationPollerIntent = new Intent(this, LocationPoller.class);  
     PendingIntent pendingIntent = PendingIntent.getBroadcast 
       (getApplicationContext(), REQUEST_CODE, locationPollerIntent, 0); 

     alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
          SystemClock.elapsedRealtime(), 6000, pendingIntent); 
    } 
} 

LocationPoller 목적은 장치의 위치를 ​​얻기 위해 서비스를 시작합니다. 그래서 alarmManager를 사용하여 특정 주파수에서이 검색을 반복합니다.

그런 다음 BroadcastReceiver에서 내 위치를 얻으면 위치를 표시하고 알림을 시작하기 위해 다른 활동을 호출합니다.

현재 위치 검색을 중단하고 싶습니다. 그래서 alarmManager를 취소하려고합니다.

public class Notifier extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.alarm_notifier); 

     findViewById(R.id.btTurnOffAlarm). 
           setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent intentToStop = new Intent(this, LocationPoller.class); 
       PendingIntent pendingIntentToStop = PendingIntent.getBroadcast 
         (getApplicationContext(), REQUEST_CODE, intentToStop, 0); 

       AlarmManager alarmManager = (AlarmManager) 
           getSystemService(ALARM_SERVICE); 
       alarmManager.cancel(pendingIntentToStop); 
      } 
     }); 
    } 
} 

하지만 작동하지 않는 경우 계속해서 위치를 검색합니다.

실마리가 있습니까?

감사합니다.

답변

3

고유 한 인 텐트를 만들어야합니다.

STH 유사에 : 당신에게 맞는 알람을 다시 가져올 때

locationPollerIntent.setData((Uri.parse("YOUR UNIQUE ID FOR THIS INTENT"))); 

알람 관리기는 의도의 데이터 필드를 확인합니다. 은 의도 한 데이터에 대해 고유 한 Uri를 설정하기 만합니다. 당신이 당신의 알람을 다시 얻을 때

때문에, STH 좋아해 :

intentToStop.setData((Uri.parse("YOUR SAME UNIQUE ID FOR THIS INTENT"))); 

알람이 후 취소해야합니다.

+0

답장을 보내 주셔서 감사합니다. 시도했지만 작동하지 않았습니다. – blui

+0

각 테스트 전에 에뮬레이터를 지우셨습니까?어쩌면 이전 알람이 여전히 위치를 가져 오는 중일 수 있습니다. 각 알람이 고유 ID를 사용하는지 확인하십시오. 그래서 알람을 5 번 발사하면 5 개의 ID가 필요합니다 ... 그냥 추측하면 ... –

1

REQUEST_CODE은 두 위치에서 동일해야합니다. 코드 목록에 정의 된 위치가 표시되지 않습니다.

또한 getApplicationContext()은 필요하지 않습니다. this은 정상적으로 작동합니다.

+0

답장을 보내 주셔서 감사합니다. – blui

+0

예, REQUEST_CODE는 동일합니다. 나는 경보를 취소하면 서비스를 중단해야하는지 알고 싶습니다. alarmManager.cancel()을 호출 한 후 계속해서 위치를 검색하기 때문에. 감사합니다. – blui

+1

@ bloi : "알람을 취소하면 서비스를 중지해야합니다."- 아니요. 알람을 취소하는 것은'PendingIntent'가 호출되는 것을 멈추는 것입니다. – CommonsWare

0

동일한 인스턴스를 전달해야합니다. 당신이 두 개의 서로 다른 참조는 의도를 만들 때마다이있는 경우

public static final Uri MYURI = Uri.parse("Some_Uri"); 
[...] 
locationPollerIntent.setData(MyClass.MYURI); 
[...] 
intentToStop.setData(MyClass.MYURI); 

, 그것은 작동하지 않습니다. 여기

보다 상세한 예 : http://malubu.wordpress.com/2012/06/05/take-your-time-widgets-and-alarmmanager/

+0

나는 같은 문제가있다, 나는 의도를위한 싱글 톤을 만들지 만 여전히 작동하지 않는다 ... – Lilo

0

나는 동일한 문제가 있었다, 그 아니라 의도 "locationPollerIntent"와 "intentToStop"의 동일한 인스턴스를 가지고 있지만 동일한 인스턴스를해야합니다에 대한 PendingIntent.

AlarmManager를 중지 한 후에도 서비스가 중지됩니다.

static PendingIntent instance; 

public static PendingIntent getInstance(Context context) { 

    if (instance == null) 
    { 
     Intent intent = new Intent(context, LocationPoller.class); 
     instance = PendingIntent.getService(context, REQUEST_CODE, intent, 0); 
    } 

    return instance; 
} 
관련 문제