2011-08-19 5 views
1

setRepeating() 알람이 일정 시간대에 있다면 BroadcastReceiver으로 비활성화하고 싶습니다. 나는 BroadcastReceiver의 내부에서 그것을 사용하지 않을 것이므로 가능한 경우 그것이 나의 첫 번째 선택이 될 것입니다. 에서안드로이드 반복 알람의 나이를 알려주는 방법은 무엇입니까?

Activity :

에서
// Start our alarm 
Intent intent = new Intent(Main.this, Receiver.class); 

long firstTime = SystemClock.elapsedRealtime(); 
firstTime += 2 * 1000; // start in 2 seconds 
long interval = 2000; // 2 seconds for testing 

intent.putExtra("start", firstTime); // Tell the receiver the creation date (not working) 

PendingIntent sender = PendingIntent.getBroadcast(Main.this, 0, intent, 0); 

// Schedule the alarm! 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, interval, sender); 

BroadcastReceiver : 나에게 이제

08-19 11:01:04.420: INFO/(1371): Alarm Running for 0 
08-19 11:01:06.420: INFO/(1371): Alarm Running for 0 
08-19 11:01:08.430: INFO/(1371): Alarm Running for 0 
08-19 11:01:10.420: INFO/(1371): Alarm Running for 0 
08-19 11:01:12.419: INFO/(1371): Alarm Running for 0 
08-19 11:01:14.419: INFO/(1371): Alarm Running for 0 
08-19 11:01:16.420: INFO/(1371): Alarm Running for 0 
08-19 11:01:18.420: INFO/(1371): Alarm Running for 0 

가 의도하지 않는 것을 의미한다

내가 이것을 볼 로그 캣에서
@Override 
public void onReceive(Context context, Intent intent) { 
    // ... alarm stuff 
    long now = SystemClock.elapsedRealtime(); 
    Log.i("", "Alarm Running for " + (now - intent.getLongExtra("start", now))); 
    //getLongExtra() defaults to 'now' because there is no extra 'start' 
} 

.. putExtra()으로받은 "시작"추가 내용이 들어 있습니다.

알람의 나이를 의도 나 추가 방법으로 알려면 어떻게해야합니까?

편집 : 수신 클래스가 static int을 생성하고 알람 코드가 실행될 때마다이를 증가시킴으로써 수신 클래스가 브로드 캐스트를 수신 한 횟수를 알 수 있습니다. 그러나 이것은 "나이"를 찾는 좋은 방법이 아닙니다.

업데이트 : 다음과 같은 뫼비우스가 제공하는 답의 조합, 당신은 또한, PendingIntent.getBroadcast()PendingIntent.FLAG_UPDATE_CURRENT를 통과해야합니다 :

PendingIntent sender = PendingIntent.getBroadcast(Main.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

답변

1

번들을 사용해 보셨습니까?

onReceive() 내부

Bundle myBundle = intent.getExtras(); 
    if (myBundle != null) { 
     long startTime = myBundle.get("start"); 
} 
1

잘 모르겠어요 경우 onReceive에서 두 번째 인수() 메소드가있다 활동의 의도. 당신의 활동이 아니라 수신기를 발사하는 AlarmManager. 내가 옳다면, 내 마음은 당신의 문제에 대한 단 하나의 해결책을 가지고 있습니다 - 당신의 경보의 시작 시간을 포함하는 외부 적으로 저장된 파일.

은 다음과 같이 진행됩니다

  1. Activity :

    // Remember about adding 
    // <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    // in your manifest xml 
    File file = new File(Environment.getExternalStorageDirectory() + "/yourAppsName", 
         ".alarmStartTime"); 
    try { 
        BufferedWriter out = new BufferedWriter(new FileWriter(file.getAbsolutePath(), true)); 
        out.write(SystemClock.elapsedRealtime()); 
        out.close(); 
    } catch (IOException e) { 
        Toast.makeText(this.getApplicationContext(), "Problem", Toast.LENGTH_SHORT).show(); 
    } 
    
  2. BroadcastReceiver :

    File file = new File(Environment.getExternalStorageDirectory() + "/yourAppsName", 
         "alarmStartTime.txt"); 
    String content = ""; 
    try { 
        content = new Scanner(file).useDelimiter("\\Z").next(); 
    } catch (FileNotFoundException e) { 
        Toast.makeText(this.getApplicationContext(), "Problem", Toast.LENGTH_SHORT).show(); 
    } 
    Log.i("", "Alarm Running for " + (SystemClock.elapsedRealtime() - Long.decode(content))); 
    

, 비록 당신이 원하는 경우 자사의 CPU와 배터리 킬러 그것을 실행 2 초마다)

왜이 정적 변수를 사용할 수 없습니까? 특정 간격의 알람이있는 경우 반복 횟수를 사용하면 수신기의 수명을 알 수 있습니다.

+0

내가 할 수있는,이 테스트 목적으로 만이기 때문에 정말 내가 그것을 어떻게 구현 하느냐는 중요하지 않습니다) 단순히 호기심이 시간 I에 질문을 정적 var 이외의 솔루션을 구현할 필요가 없습니다. 이것은 일반적으로 '과잉 공격'으로 인해 용인 할만한 솔루션이 아니지만 실행 가능한 솔루션에 +1입니다. – styler1972

관련 문제