2014-09-28 168 views
0

我試圖讓報警管理器觸發,但它不會,我不明白爲什麼。 這裏是我在我的主要活動代碼:報警管理器不會觸發

public void scheduleAlarm() 
{  
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 11); 
    calendar.set(Calendar.MINUTE, 32); 
    Intent intentAlarm = new Intent(this, AlarmReceiver.class); 

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

    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), PendingIntent.getBroadcast(this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT)); 
} 

,這是我在我的課alarmreceiver:

public class AlarmReceiver extends BroadcastReceiver { 
NotificationCompat.Builder mBuilder; 
@Override 
public void onReceive(Context context, Intent intent) { 
    Toast.makeText(context, "You got pawned", Toast.LENGTH_LONG).show(); 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("My notification") 
      .setContentText("Hello World!"); 

    // Sets an ID for the notification 
    int mNotificationId = 001; 
    // Gets an instance of the NotificationManager service 
    NotificationManager mNotifyMgr = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    // Builds the notification and issues it. 
    mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
} 

}

的人都知道爲什麼它不工作?

在此先感謝!

+0

是您的代碼進入的onReceive()?你有沒有在Manifest.xml文件中註冊你的AlarmReceiver? – joao2fast4u 2014-09-28 12:57:30

+0

對不起,麻煩了,這是我在Manifest.xml中跟着的教程中的一個錯字 – lguenier 2014-09-28 13:12:16

回答

1

所以不要忘記添加在你下面的代碼Android清單

<receiver android:name=".AlarmReceiver"/> 

我的問題是在這部分錯字......