2014-09-28 103 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"/> 

我的问题是在这部分错字......