我需要在我的应用中设置一个固定时间的闹钟,用户将收到通知(提醒对话框)以从app&再次登录。我正在尝试使用报警管理器。下面是代码,我试过,但它没有给出任何通知。
在活动中设置闹钟:
loginAlarm = new LoginAlarmReceiver();
loginAlarm.setAlarm(NewDashboardActivity.this);
LoginAlarmReceiver
public class LoginAlarmReceiver extends WakefulBroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, LoginActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
context,
0,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context).setContentTitle("Login")
.setContentText("Please logout/login again")
//.setTicker("New Message Alert!")
.setSmallIcon(R.drawable.*****)
.setSound(defaultSoundUri)
.setAutoCancel(true)
;
notification.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(001, notification.build());
Utils.alertDialogShow(context,context.getString(R.string.login_message));
}
// BEGIN_INCLUDE(set_alarm)
public void setAlarm(Context context) {
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, LoginAlarmReceiver.class);
alarmIntent = PendingIntent.getService(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
//calendar.setTimeInMillis(System.currentTimeMillis());
// Set the alarm's trigger time to 8:30 a.m.
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 10);
calendar.set(Calendar.SECOND, 0);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
// END_INCLUDE(set_alarm)
// BEGIN_INCLUDE(cancel_alarm)
public void cancelAlarm(Context context) {
if (alarmMgr!= null) {
alarmMgr.cancel(alarmIntent);
}
}
// END_INCLUDE(cancel_alarm)
}
我尝试在"OnReceive"
处设置断点但是在设定时间永远不会被调用。上面的代码是上午9:10。
在清单文件中,警报接收器的定义如下:
<receiver android:name=".broadcast.LoginAlarmReceiver">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.WAKE_LOCK">
</uses-permission>
我检查了设备上的应用设置,并为该应用启用了通知。请告知我缺少的东西。