2014-07-17 39 views
0

我試着報警,並已撞牆。我不認爲我的鬧鐘設置正確,因爲在鬧鐘響起後我再也沒有得到確認。以下是我呼籲告警管理:報警管理器不執行廣播級

long alarmtime=new GregorianCalendar().getTimeInMillis()+10*1000;//run after 10 seconds 
Intent i = new Intent(this, AlarmReceiver.class); 
     PendingIntent pi = PendingIntent.getService(this, 0, i, 0); 
     AlarmManager alarmman = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmman.cancel(pi); // cancel any existing alarms 

     alarmman.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
       alarmtime, 10 * 1000, pi);//run every 10 seconds 

這是我的AlarmReceiver.java:

public class AlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "TEST", Toast.LENGTH_LONG).show(); 
    } 

} 

然而,測試文本沒有出現,我想不通爲什麼。

回答

0

由於您使用的是AlarmManager.ELAPSED_REALTIME_WAKEUP的說法,你最初的報警時間應底座上的設備經過實時:

long alarmtime = SystemClock.elapsedRealtime() + 10 * 1000; 
alarmman.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmtime, 10 * 1000, pi); 

(見this link)。

對於BroadcastReceiver它可能是PendingIntent.getBroadcast()而不是PendingIntent.getService()。您還取消了報警,只是更新您的PendingIntent這樣,並嘗試之前沒有取消:

PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); 

閱讀documentation徹底瞭解更多信息。

+0

謝謝,但我還沒有得到它的工作。 – Bonhomme

+0

我還在編輯,請參閱'getBroadcast()'部分。 – Blacklight

+0

剛剛嘗試過,它也不起作用。我甚至沒有收到任何錯誤消息。 – Bonhomme

0

確保通過執行System.out.println("TEST");而不是Toast調用BroadcastReceiver。如果你能夠看到你的logcat,那麼問題可能是你需要在UI線程中運行Toast。

+0

使用println嘗試後,Logcat空了。 – Bonhomme