LocalBroadcastManager 的使用

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

代码

 1 package com.qf.broadcastreceiver06;
 2 
 3 import android.app.Activity;
 4 import android.app.Notification;
 5 import android.app.NotificationManager;
 6 import android.content.BroadcastReceiver;
 7 import android.content.Context;
 8 import android.content.Intent;
 9 import android.content.IntentFilter;
10 import android.graphics.BitmapFactory;
11 import android.os.Bundle;
12 import android.support.v4.app.NotificationCompat;
13 import android.support.v4.content.LocalBroadcastManager;
14 import android.view.View;
15 
16 public class MainActivity extends Activity {
17 
18     LocalBroadcastManager localBroadcastMgr;//本地广播管理器
19     
20     MyReceiver myReceiver;
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25         
26         //获取本地广播管理器对象
27         localBroadcastMgr=LocalBroadcastManager.getInstance(getApplicationContext());
28         
29         myReceiver=new MyReceiver();
30         //注册本地广播接收器
31         localBroadcastMgr.registerReceiver(myReceiver, new IntentFilter("com.qf.broadcast.disen"));
32     }
33 
34     public void sendBroadcast(View v){//发送本地广播
35         
36         Intent intent=new Intent("com.qf.broadcast.disen");
37         
38         //通过本地广播管理器来发送广播
39         localBroadcastMgr.sendBroadcast(intent);
40     }
41     
42     
43     @Override
44     protected void onDestroy() {
45         super.onDestroy();
46         
47         //取消注册本地广播接收器
48         localBroadcastMgr.unregisterReceiver(myReceiver);
49     }
50     
51     //定义广播接收器
52     class MyReceiver extends BroadcastReceiver{
53         @Override
54         public void onReceive(Context context, Intent intent) {
55             // TODO Auto-generated method stub
56             //发送通知
57             
58             //获取系统的通知管理器组件
59             NotificationManager notifyMgr=
60                     (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
61         
62             //实例化广播构造器
63             NotificationCompat.Builder builder=
64                     new NotificationCompat.Builder(getApplicationContext());
65             
66             //设置通知的小图标、标题、内容、滚动内容、动作等
67             builder.setSmallIcon(android.R.drawable.ic_menu_call)
68                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
69                    .setContentTitle("提示")
70                    .setContentText("最新消息:暂定今天下午5点开会,请准时回来.....")
71                    //设置通知在状态栏里滚动
72                    .setTicker("最新消息:暂定今天下午5点开会,请准时回来.....最新消息:暂定今天下午5点开会,请准时回来.....最新消息:暂定今天下午5点开会,请准时回来.....")
73                    .setPriority(NotificationCompat.PRIORITY_MAX)
74                    .setDefaults(Notification.DEFAULT_SOUND);
75                    
76             notifyMgr.notify(1, builder.build());
77             
78         }
79     }
80 }
MainActivity.java

效果如下:

原文地址:https://www.cnblogs.com/bimingcong/p/4820434.html