我的活动中有这个:
AlarmManager alarmMgr;
PendingIntent alarmIntent;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 20);
calendar.set(Calendar.SECOND, 00);
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
alarmIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 123456, intent, 0);
alarmMgr = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
alarmIntent);
Toast.makeText(this, "Alarm added", Toast.LENGTH_SHORT).show();
这是我的广播接收器类:
@Override
public void onReceive(Context context, Intent intent) {
Intent mIntent = new Intent(context, AlarmMessageActivity.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
Toast.makeText(context, "Alarm", Toast.LENGTH_SHORT).show();
Vibrator vibrator = (Vibrator) context
.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
我还将这些添加到清单文件
<activity
android:name=".AlarmMessageActivity"
android:label="@string/title_activity_alarm_message" >
</activity>
<receiver android:name=".AlarmBroadcastReceiver" >
</receiver>
AlarmMessageActivity类只是一个显示文本的简单页面。
以上代码适用于sdk 8.但是对于Kitkat它不起作用,我在设置闹钟或广播接收器执行时也没有出现任何错误。我希望所有版本的闹钟都能正常工作,我的min sdk是8。
有谁能告诉我哪里可能错了。我是android的新手。