如何通过后台服务在android中的特定时间每天重复通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23440251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to repeat notification daily on specific time in android through background service
提问by user3458918
Hi I am working on application where I have set the notification on user entered date and time through background service. Now I want to set notification/alarm daily at 6 pm to ask user does he want to add another entry? How can I achieve this? Should I use the same background service or Broadcast receiver? Please give me better solution for that and tutorial will be great idea. Thanks in advance.
嗨,我正在开发应用程序,我通过后台服务设置了用户输入日期和时间的通知。现在我想每天下午 6 点设置通知/闹钟,询问用户是否要添加另一个条目?我怎样才能做到这一点?我应该使用相同的后台服务还是广播接收器?请给我更好的解决方案,教程将是个好主意。提前致谢。
回答by Mr. N.V.Rao
First set the Alarm Manager as below
首先设置报警管理器如下
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Create an Broadcast Receiver Class "AlarmReceiver" in this raise the notifications when onReceive
在此创建一个广播接收器类“AlarmReceiver”,在 onReceive 时引发通知
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, EVentsPerform.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.applogo)
.setContentTitle("Alarm Fired")
.setContentText("Events to be Performed").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
and in the manifest file, register receiver for the AlarmReceiver class:
并在清单文件中,为 AlarmReceiver 类注册接收器:
<receiver android:name=".AlarmReceiver"/>
No special permissions are required to raise events via alarm manager.
通过警报管理器引发事件不需要特殊权限。
回答by tommy
N.V.Rao's answer is correct, but don't forget to put the receiver
tag inside the application tag in the AndroidManifest.xml file:
NVRao 的回答是正确的,但不要忘记将receiver
标签放在 AndroidManifest.xml 文件中的应用程序标签内:
<receiver android:name=".alarm.AlarmReceiver" />