android 实现定时通知,在Android中创建定时通知(例如,针对事件)

对于某些Android应用程序,我想集成以下功能:

用户可以定义他想要被提醒的时间.当时间到了,应用程序应该在通知栏中创建通知,即使此时用户没有使用该应用程序.

为此,需要查看AlarmManager,NotificationManager和Notification.Builder类,对吧?

那么我该如何提前创建定时通知呢?我的代码(到目前为止)是这样的:

将其添加到AndroidManifest下以注册广播接收器:

创建一个新的类文件来处理它收到的警报:

public class AlarmNotificationReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

Bundle extras = intent.getExtras();

if (extras != null) {

String additionalData = extras.getString("displayText");

// show the notification now

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification mNotification = new Notification(R.drawable.ic_launcher, context.getString(R.string.app_name), System.currentTimeMillis());

PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); // open MainActivity if the user selects this notification

mNotification.setLatestEventInfo(context, context.getString(R.string.app_name), additionalData, pi);

mNotification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_SOUND;

mNotificationManager.notify(1, mNotification);

}

}

}

使用此代码(例如在MainActivity中)将警报设置为3秒后:

Intent i = new Intent(this, AlarmNotificationReceiver.class);

i.putExtra("displayText", "sample text");

PendingIntent pi = PendingIntent.getBroadcast(this.getApplicationContext(), 234324246, i, 0);

AlarmManager mAlarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

mAlarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3*1000, pi);

为了使这项工作,我需要改变什么?谢谢!

这两个问题是:

>当我在代码中更改通知时,通知的文本不会更改.它只会在我更改PendingIntent.getBroadcast(…)中的requestCode时更改.这个请求代码到底是什么?可以是随机值还是0?

>重新启动手机后,“计划”通知或警报消失.但是现在我已经看到这是正常行为,对吗?我怎么能绕过这个呢?

解决方法:

对第1部分不确定,但对于第2部分,一般方法是拦截BOOT_COMPLETED意图并使用它重新注册所有警报.遗憾的是,对于您在警报管理器中注册的每个警报,您都必须将其存储在应用程序的数据库中.

因此,您需要一个广播接收器来拦截BOOT_COMPLETED意图:

public class BootReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

// get your stored alarms from your database

// reregister them with the alarm manager

}

}

要获取BOOT_COMPLETED意图,您必须在清单中添加以下权限:

并且BootReceiver还需要使用以下intent过滤器在您的清单中注册:

android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

重要的是要注意,如果您的应用程序安装到SD卡,它永远不会收到BOOT_COMPLETED意图.此外,值得注意的是,这种实现有点天真,因为它在启动时立即执行代码,这会在启动时降低用户的手机速度.因此,我建议在拦截启动意图后将执行延迟几分钟.

标签:android,notifications,alarmmanager

来源: https://codeday.me/bug/20190521/1146859.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值