LocalBroadcastManager 的使用
一、使用本地广播发送一条广播(本例为自己发送自己接收,本地广播也可以是其他应用接收)然后接收到广播时回调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 的使用的更多相关文章
- Android LocalBroadcastManager 的使用总结
转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6048369.html 本文出自[赵彦军的博客] 前言 在Android中,Broadcast是一种广泛运用的 ...
- LocalBroadcastManager 的实现原理,Handler还是 Binder?
原文: http://www.trinea.cn/android/localbroadcastmanager-impl/ 对 LocalBroadcastManager 大家应该都不陌生,相对 Bro ...
- 使用LocalBroadcastManager
Android中BroadcastReceiver主要用途有 发送通知,更新UI或者数据,应用程序间相互通信,监听系统状态(比如开机,网络等) Android中BroadcasetReceiver的注 ...
- Android LocalBroadcastManager 与 BroadcastReceiver
Android中BroadcastReceiver主要用途有 发送通知,更新UI或者数据,应用程序间相互通信,监听系统状态(比如开机,网络等) Android中发送广播的方式: 普通广播:无论优先级大 ...
- LocalBroadcastManager 使用小解
最近在开发平板项目,完全是fragmentactivity+fragment的结构.看起来似乎简单,但是和以前不同的是,业务逻辑非常复杂,多处的非常规跳转,fragment之间的数据交换,一处更新多处 ...
- [Android Pro] Android 之使用LocalBroadcastManager解决BroadcastReceiver安全问题
参考博客: http://blog.csdn.net/t12x3456/article/details/9256609 http://blog.csdn.net/lihenair/article/de ...
- Android 中LocalBroadcastManager的使用方式
Android 中LocalBroadcastManager的使用方式 在android-support-v4.jar中引入了LocalBroadcastManager,称为局部通知管理器,这种通知的 ...
- Android 之使用LocalBroadcastManager解决BroadcastReceiver安全问题
在Android系统中,BroadcastReceiver的设计初衷就是从全局考虑的,可以方便应用程序和系统.应用程序之间.应用程序内的通信,所以对单个应用程序而言BroadcastReceiver是 ...
- LocalBroadcastManager—创建更高效、更安全的广播
前言 在写Android应用时候,有时候或多或少的需要运用广播来解决某些需求,我们知道广播有一个特性,就是使用sendBroadcast(intent);发送广播时,手机内所有注册了Broadcast ...
- 本地广播 localBroadcastManager Android
使用localBroadcastManager发出的广播只能在本应用程序的内部进行传递. App应用内广播可理解为一种局部广播,广播的发送者和接收者都同属于一个App. 相比于全局广播(普通广播),A ...
随机推荐
- 操他妈的,jquery1.4以上不能用toggle()轮流切换函数
query 1.9里面已经删除了toggle(fn1, fn2)函数 (2013-05-07 13:44:27) 转载▼ 标签: it 分类: js jquery 1.9里面已经删除了toggle(f ...
- Mac parallels desktop安装windows,linux
前言 这款软件你就看作是虚拟机vm,如果你要安装win10系统,请下载ios镜像文件 下载准备工作 Parallels Desktop 13 破解版本 联系站长所要 win10 iso镜像文件 ...
- EC20 MODULE serial com log in passwd
ec20 module would print debug info via debug uart, and you can log in by user root, the passwd is qu ...
- MiniDump产生工具
1:分析程序异常等等信息,在入口处初始化即可 //生成Dump文件信息 OS:Windows #pragma once #include <windows.h> #include < ...
- 根据inode编号来删除文件或目录
在Linux系统上,有时候会出现文件名为特殊字符的文件或目录,当我们使用rm来删除这样的文件或目录时,就会出错导致删不掉.但是我们可以依据inode号来删除这样的文件,方法如下: (1)执行ls -i ...
- Java 中 HashMap 初始化时赋值
1.HashMap 初始化的文艺写法 HashMap 是一种常用的数据结构,一般用来做数据字典或者 Hash 查找的容器.普通青年一般会这么初始化:HashMap<String, Strin ...
- Python小代码
from bs4 import BeautifulSoup import requests url = 'http://www.tripadvisor.cn/Attractions-g60763-Ac ...
- VB ListView罗列图片
一开始听到job的要求,还真不知道如何下手,用Gridview?好像有做不到把图片show出来的.在网上搜索一遍,发现原来是ListView与Imglist的结合应用. 看设计: Private ...
- psc格式的文件是什么
psc格式的文件是什么 是navicat 这个工具导出的数据文件 可以使用备份还原功能 提取sql
- 使MySQL查询区分大小写的实现方法
发布:mdxy-dxy 字体:[增加 减小] 类型:转载 我们在MySQL中使用SELECT语句查询时,可不可以使查询区分大小写?今天从网络上找到了方法,现总结如下. 1.一种方法是可以设置表或行 ...