我正在创建一个应用程序,它应该在正确的时间根据日期显示通知。但是通知来得晚或者是在打开应用程序之后。我请求您帮助我,指出代码中的错误,并对其不正确的原因进行注解。
androidmanifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.SET_ALARM" />
<receiver
android:name="com.zalj.schedule.MyNotifications.MyAlarm"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
</receiver>
我按如下方式设置闹钟:
private static void setAlarm(
Context context,
int id,
Action action){
Calendar calendar = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
calendar.set(Calendar.HOUR_OF_DAY, action.getStartHour());
calendar.set(Calendar.MINUTE, action.getStartMinute());
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(context, MyAlarm.class);
//Устанавливаем команду
intent.putExtra(
IntentHelper.COMMAND,
IntentHelper.COMMAND_NOTIFICATION_SetAlarm);
//ChanelId
intent.putExtra(
IntentHelper.CHANEL_ID,
CHANEL_ID_ACTION);
//ChanelName
intent.putExtra(
IntentHelper.CHANEL_NAME,
context.getString(R.string.Notification_chanelName_Action));
//ID
intent.putExtra(
IntentHelper.NOTIFICATION_ID,
NOTIFICATION_ID_ACTION);
//Title
intent.putExtra(
IntentHelper.NOTIFICATION_TITLE,
action.getTitle());
//Message
intent.putExtra(
IntentHelper.NOTIFICATION_MESSAGE,
action.getMessage());
PendingIntent pIntent =
PendingIntent.getBroadcast(
context,
id,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//Set alarm
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
else
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
}
myalarm类:
public class MyAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int idCase = intent.getIntExtra(
IntentHelper.COMMAND,
IntentHelper.COMMAND_NOTIFICATION_UpdateAlarmToDay);
switch (idCase){
case IntentHelper.COMMAND_NOTIFICATION_SetAlarm:{
showNotification(context ,intent);
}break;
case IntentHelper.COMMAND_NOTIFICATION_UpdateAlarmToDay:{
updateAlarmsToNextDay(context, intent);
}break;
}
}
private void showNotification(Context context, Intent intent){
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
String title;
String message;
String chanelId;
String chanelNameDiscipline;
int notificationId;
title = intent.getStringExtra(IntentHelper.NOTIFICATION_TITLE);
message = intent.getStringExtra(IntentHelper.NOTIFICATION_MESSAGE);
chanelId = intent.getStringExtra(IntentHelper.CHANEL_ID);
chanelNameDiscipline = intent.getStringExtra(IntentHelper.CHANEL_NAME);
notificationId = intent.getIntExtra(IntentHelper.NOTIFICATION_ID, 0);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, chanelId);
notification
.setSmallIcon(R.drawable.discipline_notification)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle())
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSound(NotificationHelper.getSound())
.setVibrate(NotificationHelper.getVibrate(NotificationHelper.LONG_VIBRATE))
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel nc =
new NotificationChannel(
chanelId,
chanelNameAction,
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(nc);
}
notificationManager.notify(notificationId, notification.build());
}
private void updateAlarmsToNextDay(Context context, Intent intent){
Action action;
String name = intent.getStringExtra(IntentHelper.ACTIONS_NAME);
if (name == null){
SharedPreferences settings = context.getSharedPreferences(
DataContract.MyAppSettings.LAST_VIEWED_ACTIONS,
Context.MODE_PRIVATE);
name = settings.getString(
DataContract.MyAppSettings.LAST_ACTIONS,
DataContract.MyAppSettings.NULL);
}
ScheduleBuilder scheduleBuilder = ScheduleBuilder.getInternalSchedule(context, name);
schedule = scheduleBuilder.build();
schedule.updateTimes(context);
schedule.updateActions(context, Calendar.getInstance());
MyDisciplineNotificationManager.updateAllAlarm(context, schedule);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!