我有代码:

public void AlarmStart() {

Calendar cal = Calendar.getInstance();

cal.add(Calendar.MINUTE, 5);

Intent intent = new Intent(MainNote.this, AlarmReceiver.class);

intent.putExtra("alarm_message", "MESS");

PendingIntent sender = PendingIntent.getBroadcast(MainNote.this, 1,

intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

}

它按时调用AlarmReceiver类.

public class AlarmReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

Bundle bundle = intent.getExtras();

String message = bundle.getString("alarm_message");

NotifierHelper.sendNotification(?????, MainNote.class, "ba", "baba",

2, true, true);

} // Problem here

}

然后NotifierHelper类:

public class NotifierHelper {

private static final int NOTIFY_1 = 0x1001;

public static void sendNotification(Activity caller,

Class> activityToLaunch, String title, String msg,

int numberOfEvents, boolean flashLed, boolean vibrate) {

NotificationManager notifier = (NotificationManager) caller

.getSystemService(Context.NOTIFICATION_SERVICE);

final Notification notify = new Notification(R.drawable.icon, "",

System.currentTimeMillis());

notify.icon = R.drawable.icon;

notify.tickerText = "New Alerts";

notify.when = System.currentTimeMillis();

notify.number = numberOfEvents;

notify.flags |= Notification.FLAG_AUTO_CANCEL;

if (flashLed) {

// add lights

notify.flags |= Notification.FLAG_SHOW_LIGHTS;

notify.ledARGB = Color.CYAN;

notify.ledOnMS = 500;

notify.ledOffMS = 500;

}

if (vibrate) {

notify.vibrate = new long[] { 100, 200, 200, 200, 200, 200, 1000,

200, 200, 200, 1000, 200 };

}

Intent toLaunch = new Intent(caller, activityToLaunch);

PendingIntent intentBack = PendingIntent.getActivity(caller, 0,

toLaunch, 0);

notify.setLatestEventInfo(caller, title, msg, intentBack);

notifier.notify(NOTIFY_1, notify);

}

}

如何从AlarmReceiver传递Activity调用者?

解决方法:

我认为你不需要在NotifierHelper中引用任何Activity.使用Context(Activity是其子类),例如:

public static void sendNotification(Context caller, ...

getSystemService()等方法实际上是由Context公开的.

因为你在AlarmReceiver.onReceive()中传递了一个Context,你可以传递它.

标签:android,android-activity,notifications,alarm

来源: https://codeday.me/bug/20190518/1128417.html

android receiver 通知,android – 来自BroadcastReceiver的呼叫通知相关推荐

  1. android 通知栏进度_Android改造下载图片进度通知

    android 通知栏进度 In this tutorial, we'll be using the Retrofit library in order to download an image fr ...

  2. Android中集成Jpush实现推送消息通知与根据别名指定推送附示例代码下载

    场景 经常会有后台服务向Android推送消息通知的情况. 实现 首先在Android Studio中新建一个Android应用 在Project根目录的build.gradle中配置了jcenter ...

  3. Android提高篇1 之 BroadcastReceiver 应用程序间通信的手段

    BroastcastReceiver,Broastcast是应用程序间通信的手段.BroastcastReceiver也是跟Intent紧密相连的,动态/静态注册了BroastcastReceiver ...

  4. android alarmmanager 收不到广播,android定时闹钟:Service+BroadcastReceiver+AlarmManager+NotificationManager...

    在开发android的过程中,需要一个定时提醒的功能,原本以为是一个很简单的功能,但是发现网上能找到的讲解或者代码都很或多或少地缺少一部分的设置和关键代码,所以我才注册了简书账户,分享一下. 在研究的 ...

  5. android 屏蔽 广播,Android中使用BroadcastReceiver打开和关闭WIFI

    由于自动化测试需要,我们希望能够简单的控制Android手机的WIFI开和关,而不是通过UI操作的方式.由于每个Android机型的UI都千差万别,所以需要找到一个通用得方式来满足我们的需求. 最开始 ...

  6. android 通知历史,Android 4.3人性新功能:查看通知历史

    Android系统现在的更新步伐和速度明显慢了下来,4.1.4.2.4.3都统一在Jelly Bean一个代号下就是明证.现在,Android 4.3已经泄露出来,仍然没有太大的惊喜,只是一些细节上的 ...

  7. android 广播反注册后,BroadcastReceiver注册、使用及其权限

    首先声明一个类,此类继承自BroadcastReceiver类,处理Android当中发出的广播事件: public class SMSReceiver extends BroadcastReceiv ...

  8. Android开发笔记(五十二)通知推送Notification

    PendingIntent 准备工作复习一下PendingIntent,前面的博文< Android开发笔记(五十)定时器AlarmManager>已经提到了它.PendingIntent ...

  9. Android开机自动运行APP——BroadcastReceiver

    前言: 有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以"Don't call me, I' ...

最新文章

  1. easyui ajax获取表单数据,easyui提交form表单接受数据处理、
  2. 深入理解javascript系列(十七):函数柯里化
  3. windows socket 简单实例
  4. 58 SD配置-科目分配-定义科目代码
  5. RFC2616-HTTP1.1-Methods(方法规定部分—单词注释版)
  6. Web前端工程师,互联网行业,炙手可热的翘楚!
  7. 解决js动态改变dom元素属性后页面及时渲染问题
  8. UML之旅店预订系统
  9. 秀米图文编辑对接UEditor富文本编辑器样式丢失
  10. brctl 使用说明
  11. ES-07-ElasticSearch常用插件
  12. 智星云服务器之云主机使用教程简记
  13. 【Unity】2D摄像机跟踪
  14. 2.4G NRF24L01无线模块总结
  15. php5编译安装常见错误和解决办法集锦
  16. 【Linux】centos 7中,开机不执行rc.lcoal中的命令
  17. WebRTC Video JitterBuffer
  18. Android原生集成MUI框架进行混合开发
  19. python arcade库_python游戏库:Arcade教程(4)
  20. 尚学堂Java培训:如何读书?

热门文章

  1. 2022-2028年中国热熔胶产业竞争现状及发展规模预测报告
  2. Bert代码详解(一)重点详细
  3. PyCharm 使用技巧
  4. pytorch方法,Tensor及其基本操作_重点
  5. 华为计算平台MDC810发布量产
  6. MindSpore部署图像分割示例程序
  7. 用户自定义协议client/server代码示例
  8. 各种经典透镜投影模型
  9. 2021年大数据Flink(二十一):​​​​​​​案例三 会话窗口
  10. Android ListView item设置分割线以及分割线宽度