在指定时间未触发警报和通知

编程入门 行业动态 更新时间:2024-10-27 23:31:19
本文介绍了在指定时间未触发警报和通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在指定时间触发通知和警报。我已经将日志信息放到控制台上,以查看是否设置了正确的时间并且还可以。但是,仍然没有触发警报。请帮忙。

I am trying to trigger a notification and an alarm at a specified time. I have put log information to console to see if the correct time is being set and it is fine. However, still the alarm is not being triggered. Please help.

/ 用于创建通知和警报的代码 /

/Code for Creating Notification and Alarm/

btn_add_task.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { db.addTask(new DataModel(input_task_title.getText().toString(),input_task_desc.getText().toString(), checkBox.isChecked(),txtDate.getText().toString(),txtTime.getText().toString(),isComplete)); Calendar calendar = Calendar.getInstance(); //Set notification for the set date and time calendar.set(Calendar.MONTH, month); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, 0); if(mHour>12) { calendar.set(Calendar.AM_PM, Calendar.PM); } else { calendar.set(Calendar.AM_PM, Calendar.AM); } Log.i("Alarm at: ",calendar.get(Calendar.DAY_OF_MONTH)+"-"+ calendar.get(Calendar.MONTH)+"-"+ calendar.get(Calendar.YEAR)+" at "+ calendar.get(Calendar.HOUR_OF_DAY)+":"+ calendar.get(Calendar.MINUTE)); Intent notifyMessage = new Intent(getApplicationContext(),NotificationMessage.class); PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0, notifyMessage, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi); Toast.makeText(getApplicationContext(),R.string.new_task_added,Toast.LENGTH_LONG).show(); startActivity(new Intent(AddTask.this,MainActivity.class)); } });

/ 通知消息类别 /

/Notification Message Class/

package com.apps.katz.doer; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.util.Log; /** * Created by katz on 4/5/16. */ public class NotificationMessage extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { showNotification(context); } private void showNotification(Context context) { Log.i("notification","visible"); PendingIntent contentIntent = PendingIntent.getActivity(context,0, new Intent(context,NotificationMessage.class),0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Doer Notification") .setContentText("This is a Doer Notification"); mBuilder.setContentIntent(contentIntent); mBuilder.setDefaults(Notification.DEFAULT_SOUND); mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); mBuilder.setDefaults(Notification.DEFAULT_LIGHTS); mBuilder.setAutoCancel(true); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(1,mBuilder.build()); } }

请帮我做错了吗?

推荐答案

看看 setRepeating 方法的html#setRepeating(int,%20long,%20long,%20android.app.PendingIntent) rel = nofollow>文档:

Take a look at the documentation of the setRepeating method:

注意:从API 19开始,所有重复的警报都是不精确的。如果您的应用程序需要精确的交付时间,则它必须使用一次性精确警报,并如上所述每次重新安排。 targetSdkVersion早于API 19的旧版应用程序将继续将其所有警报(包括重复警报)视为完全相同。

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

您的始终使用 setRepeating 警报会不准确,请尝试使用 setExact 并在触发警报时对其进行重新编程。

Your alarms will be inexact always using setRepeating, try using setExact and reprograming the alarm when it's triggered.

也可以尝试扩展 WakefulBroadcastReceiver 而不是 NotificationMessage 中的 BroadcastReceiver 可以在您的应用未运行时唤醒服务。

Also, try extending WakefulBroadcastReceiver instead of BroadcastReceiver in NotificationMessage in order to be able to wake the service when your app isn't running.

更多推荐

在指定时间未触发警报和通知

本文发布于:2023-11-24 09:56:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624749.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   通知   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!