2011-08-19 92 views
1

我有一個setRepeating()報警計劃,如果它在一定的年齡,我想在它的BroadcastReceiver禁用。我寧願在BroadcastReceiver內禁用它,所以如果它可能是我的第一選擇。如何判斷Android重複鬧鈴的年齡?

在我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

@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' 
} 

在logcat的我看到這個..

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 

現在,這對我來說意味着意圖不包含我用putExtra()給予的「開始」額外費用。

如何通過意向演員或其他方法來判斷鬧鈴的年齡?

編輯:我可以通過創建一個static int找出接收類接收廣播的次數,並在每次執行警報代碼時遞增它,但這不是找到「年齡」的好方法。

更新:在由莫比烏斯提供的答案組合,還必須通過PendingIntent.FLAG_UPDATE_CURRENTPendingIntent.getBroadcast(),就像如下:

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第二個參數()方法是來自Activity的意圖。它的警報管理器正在啓動接收器,而不是您的活動。如果我是對的,我的思想只有一個解決方案 - 文件存儲在外部,包含鬧鐘的開始時間。

它是這樣的:

  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

我就可以了,因爲這是僅用於測試目的,其實並不重要,我如何實現它;)我只是問的問題出於好奇,在這個時候,我除了靜態變量之外,沒有必要實現一個解決方案..這通常不是一個可接受的解決方案,因爲「過度殺傷」,而是可行的解決方案+1 – styler1972

相關問題