2011-12-13 90 views
0

我只是試圖獲得一個簡單的測試應用程序與AlarmManager工作。Android AlarmManager不觸發

public class TestActivity extends Activity { 
    private static final int PERIOD = 1000; 

    private AlarmManager alarmManager; 
    private PendingIntent pendingIntent; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     Log.v("TextActivity", "WHY NOT!"); 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 

     alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

     Intent intent = new Intent(this, TestReceiver.class); 
     pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 
       PendingIntent.FLAG_CANCEL_CURRENT); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
       System.currentTimeMillis(), PERIOD, pendingIntent); 

     Log.v("TestActivity", "Whee!"); 
    } 
} 

public class TestReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.v("TestReceiver", "Got Here"); 
    } 
} 

我在硬件電話和模擬器上都試過這個,我沒有看到收到的消息。我錯過了明顯的東西嗎?我對Android開發比較陌生。

+0

您是否在清單中聲明瞭接收者? – gwa

回答

0

您是否在AndroidManifest.xml中註冊了接收器?週期也是毫秒,所以每秒都會觸發警報,這真的是你想要的嗎?

+0

是的,它現在正在測試。我不知道你必須添加到清單。我會檢查一下。 – PolandSpring

+0

這是明顯的問題。 – PolandSpring