一、使用本地广播发送一条广播(本例为自己发送自己接收,本地广播也可以是其他应用接收)然后接收到广播时回调Receiver类中的回调方法onReceive()在此方法中自定义发出通知

代码

 package com.qf.broadcastreceiver06;

 import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View; public class MainActivity extends Activity { LocalBroadcastManager localBroadcastMgr;//本地广播管理器 MyReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //获取本地广播管理器对象
localBroadcastMgr=LocalBroadcastManager.getInstance(getApplicationContext()); myReceiver=new MyReceiver();
//注册本地广播接收器
localBroadcastMgr.registerReceiver(myReceiver, new IntentFilter("com.qf.broadcast.disen"));
} public void sendBroadcast(View v){//发送本地广播 Intent intent=new Intent("com.qf.broadcast.disen"); //通过本地广播管理器来发送广播
localBroadcastMgr.sendBroadcast(intent);
} @Override
protected void onDestroy() {
super.onDestroy(); //取消注册本地广播接收器
localBroadcastMgr.unregisterReceiver(myReceiver);
} //定义广播接收器
class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//发送通知 //获取系统的通知管理器组件
NotificationManager notifyMgr=
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); //实例化广播构造器
NotificationCompat.Builder builder=
new NotificationCompat.Builder(getApplicationContext()); //设置通知的小图标、标题、内容、滚动内容、动作等
builder.setSmallIcon(android.R.drawable.ic_menu_call)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setContentTitle("提示")
.setContentText("最新消息:暂定今天下午5点开会,请准时回来.....")
//设置通知在状态栏里滚动
.setTicker("最新消息:暂定今天下午5点开会,请准时回来.....最新消息:暂定今天下午5点开会,请准时回来.....最新消息:暂定今天下午5点开会,请准时回来.....")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_SOUND); notifyMgr.notify(1, builder.build()); }
}
}

MainActivity.java

效果如下:

LocalBroadcastManager 的使用的更多相关文章

  1. Android LocalBroadcastManager 的使用总结

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6048369.html 本文出自[赵彦军的博客] 前言 在Android中,Broadcast是一种广泛运用的 ...

  2. LocalBroadcastManager 的实现原理,Handler还是 Binder?

    原文: http://www.trinea.cn/android/localbroadcastmanager-impl/ 对 LocalBroadcastManager 大家应该都不陌生,相对 Bro ...

  3. 使用LocalBroadcastManager

    Android中BroadcastReceiver主要用途有 发送通知,更新UI或者数据,应用程序间相互通信,监听系统状态(比如开机,网络等) Android中BroadcasetReceiver的注 ...

  4. Android LocalBroadcastManager 与 BroadcastReceiver

    Android中BroadcastReceiver主要用途有 发送通知,更新UI或者数据,应用程序间相互通信,监听系统状态(比如开机,网络等) Android中发送广播的方式: 普通广播:无论优先级大 ...

  5. LocalBroadcastManager 使用小解

    最近在开发平板项目,完全是fragmentactivity+fragment的结构.看起来似乎简单,但是和以前不同的是,业务逻辑非常复杂,多处的非常规跳转,fragment之间的数据交换,一处更新多处 ...

  6. [Android Pro] Android 之使用LocalBroadcastManager解决BroadcastReceiver安全问题

    参考博客: http://blog.csdn.net/t12x3456/article/details/9256609 http://blog.csdn.net/lihenair/article/de ...

  7. Android 中LocalBroadcastManager的使用方式

    Android 中LocalBroadcastManager的使用方式 在android-support-v4.jar中引入了LocalBroadcastManager,称为局部通知管理器,这种通知的 ...

  8. Android 之使用LocalBroadcastManager解决BroadcastReceiver安全问题

    在Android系统中,BroadcastReceiver的设计初衷就是从全局考虑的,可以方便应用程序和系统.应用程序之间.应用程序内的通信,所以对单个应用程序而言BroadcastReceiver是 ...

  9. LocalBroadcastManager—创建更高效、更安全的广播

    前言 在写Android应用时候,有时候或多或少的需要运用广播来解决某些需求,我们知道广播有一个特性,就是使用sendBroadcast(intent);发送广播时,手机内所有注册了Broadcast ...

  10. 本地广播 localBroadcastManager Android

    使用localBroadcastManager发出的广播只能在本应用程序的内部进行传递. App应用内广播可理解为一种局部广播,广播的发送者和接收者都同属于一个App. 相比于全局广播(普通广播),A ...

随机推荐

  1. tornado.ioloop.IOLoop相关文章

    http://6167018.blog.51cto.com/6157018/1532899 http://kenby.iteye.com/blog/1159621

  2. unity3d中物体的控制

    一.物体的循环移动和旋转 思路:通过对时间的计算,每隔一段时间让物体旋转,实现来回移动. float TranslateSpeed = 0.02f; float TranslateSpeedTime ...

  3. php 编程笔记分享 - 非常实用

    php opendir()列出目录下所有文件的两个实例 php opendir()函数讲解及遍历目录实例 php move_uploaded_file()上传文件实例及遇到问题的解决方法 php使用m ...

  4. HTML(具体代码看笔记本)

    参考:https://www.cnblogs.com/liwenzhou/p/7988087.html 一, HTML   1. HTML结构   2. 标签    1. 块儿级标签     h1~h ...

  5. IDEA设置syso快捷键输出System.out.println();

    用Eclipse时间长了, 就习惯之前的快捷键! 当然, IDEA不愧是Java开发的”利器”! 写起代码就是一个字 – “爽”! 建议大家可以去尝试一下! 当然, 在IDEA中输出System.ou ...

  6. python学习之RabbitMQ-----消息队列

    RabbitMQ队列 首先我们在讲rabbitMQ之前我们要说一下python里的queue:二者干的事情是一样的,都是队列,用于传递消息 在python的queue中有两个一个是线程queue,一个 ...

  7. 代码: 日期和时间 datepicker

    bootstrap 的相关的时间插件 http://www.bootcss.com/p/bootstrap-datetimepicker/ jquery ui的日期插件 http://www.w3cs ...

  8. sbt的安装测试

    1.下载 wget https://github.com/sbt/sbt/releases/download/v0.13.15/sbt-0.13.15.tgz 2.安装 tar -zxvf sbt-0 ...

  9. 记一次python爬虫实战,豆瓣电影Top250爬虫

    import requests from bs4 import BeautifulSoup import re import traceback def GetHtmlText(url): for i ...

  10. win2008 server ping不同

    win2008 server ping不同,网络正常. 下图可以解决!!!