一、使用本地广播发送一条广播(本例为自己发送自己接收,本地广播也可以是其他应用接收)然后接收到广播时回调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. linux查看某个目录下有哪些文件的命令

    分别是ll和ls命令 ll /usr/local/lib ls /usr/local/lib

  2. 自己写的jQuery浮动广告插件

    效果图: 文件位置摆放: 插件的js代码: $.extend({ pfAdv:function(options){ var defaults={ count:1, startTop:200, star ...

  3. django的forms

    base知识 参考博文:https://www.cnblogs.com/yuanchenqi/articles/9036474.html 用户表单是Web端的一项基本功能,大而全的Django框架中自 ...

  4. 时间模块 --- time

    表示时间有三种方法:timestamps  Format String    struct-time 1 时间戳 :通常来说,时间戳表示的是从1970年一月一日00:00:00开始按秒计算的偏移量,使 ...

  5. PyQt5系列教程(九)QInputDialog的使用

    软硬件环境 Ubuntu 15.10 32bit Python 3.5.1 PyQt 5.5.1 前言 输入框是界面开发中非常常见的控件,本文就来看看PyQt5中QInputDialog的使用 实例 ...

  6. 在IDEA下使用Spring Boot的热加载(Hotswap)

    你是否遇到过这样的困扰: 当你写完一段代码后,要看到效果,必须点击IDEA的停止按钮,然后再次重启启动项目,你是否觉得这样很烦呢? 如果你觉得很烦,本文就是用来解决你的问题的. 所谓热加载,就是让我们 ...

  7. 玩转laravel5.4的入门动作(二)

    做个文章的增删改查 第一步  把数据库的表结构建好,生成迁移 1 怎么建,当然是用php artisan命令了 使用 Artisan 命令 make:migration 来创建一个新的迁移: php ...

  8. 4.Spring中使用Log4j

    转自:https://blog.csdn.net/luohai859/article/details/52250807 这里要实现web项目中利用Spring来使用Log4j (1)接上面的工程,然后 ...

  9. IOS HTTP访问端口

    Project dyld_sim raised exception class ENetHTTPClientException with message 'Error -1022 accessing ...

  10. SQL SERVER2014 安装 Error code 0x858C001B.

    原因是语言版本不一致,SQL SERVER是中文简体版,操作系统是英文版,在操作系统.控制面板,区域语言设置为中文就Ok啦. TITLE: SQL Server Setup failure.----- ...