我试图让警报管理器触发,但事实并非如此,我无法理解。 这是我在主要活动中的代码:
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));
}
这就是我班上的闹钟收发器:
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 :(得分:1)
所以不要忘记在android manifest中添加以下代码
<receiver android:name=".AlarmReceiver"/>
我的问题是这部分的错字......