通过警报管理器在特定时间创建通知

编程入门 行业动态 更新时间:2024-10-25 21:28:54
本文介绍了通过警报管理器在特定时间创建通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在特定时间创建通知.我创建了一个广播接收器并通过 AlarmManager 调用它.问题是没有收到广播并且我没有收到任何通知.

I am trying to create a notification at a particular time. Im creating a broadcast receiver and calling it through the AlarmManager. Problem is that the broadcast is not received and that I am not getting any notifications.

在清单中注册广播接收器,

Registering the Broadcast Receiver in the Manifest,

<receiver android:name="com.example.android.receivers"> <intent-filter> <action android:name="com.example.android.receivers.AlarmReceiver" /> </intent-filter> </receiver>

这是广播接收器,

public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context pContext, Intent pIntent) { // if (pIntent.getAction().equalsIgnoreCase("")) { // // } Log.d("Alarm Receiver", "onReceive called"); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(pContext).setSmallIcon(R.drawable.icon).setContentTitle("Test Notification") .setContentText("Test Notification made by Syed Ahmed Hussain"); Intent resultIntent = new Intent(pContext, CalendarView.class); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(pContext); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(CalendarView.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); notificationBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) pContext.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(1, notificationBuilder.build()); } }

来自主要活动的代码,用于创建警报.

Code from Main Activity which creates an Alarm.

/** * * @param pDate * @param pTime */ private void createNotification(String pDate, String pTime) { AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, AlarmReceiver.class); PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0); // alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60 * 1000, alarmIntent); alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), alarmIntent); }

谁能指出并纠正我做错了什么?谢谢.

Can anyone please point out and rectify what am I doing wrong? Thank you.

推荐答案

您在 Manifest 中对 Receiver 的定义有误.

You have a wrong definition of the Receiver in your Manifest.

试试这个:

<receiver android:name="com.example.android.receivers.AlarmReceiver" />

在android:name中你必须指定接收者的绝对包+类,或者你指定的package="com.example.android"的相对在清单的根元素中.例如:

in android:name you have to specify the absolute package+class of the receiver, or the relative from the package="com.example.android" you specified in the root element of the Manifest. e.g:

<manifest xmlns:android="schemas.android/apk/res/android" package="com.example.android" >

接收器的操作部分将指定要侦听的操作.所以你也可以这样做:

Action sections of receivers are to specify which actions to listen. So you can also do it this way:

基于 Action 而不是类创建 Intent

Create an Intent based on the Action instead of the class

public static final String ACTION = "com.example.android.receivers.NOTIFICATION_ALARM"; private void createNotification(String pDate, String pTime) { AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(ACTION); PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0); alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), alarmIntent); }

然后你可以在你的接收器中处理多个动作:

Then you can handle multiple actions in your receiver:

@Override public void onReceive(Context pContext, Intent pIntent){ if (pIntent.getAction().equals(ACTION)){ // Here your handle code } }

但是,您必须以这种方式在清单中声明您的接收者:

But then, you will have to declare your receiver in the manifest this way:

<receiver android:name="com.example.android.receivers"> <intent-filter> <action android:name="com.example.android.receivers.NOTIFICATION_ALARM" /> </intent-filter> </receiver>

两种方式都可以使用.

更多推荐

通过警报管理器在特定时间创建通知

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

发布评论

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

>www.elefans.com

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