1.回顾

   上篇 学习 ContentProvider 实现 两个app应用数据共享 ;


2.重点

   (1)BroadCastReceiver 重点

   (2)生命周期

   (3)具体实现


3.BroadCastReceiver


Broadcast是一种广泛运用的在应用程序之间传输信息的机制 .(ContentProvider)


BroadcastReceiver是对发送出来的 Broadcast进行过滤接受并响应的一类组件.


BroadcastReceiver 自身并不实现图形用户界面,但是当它收到某个通知后, BroadcastReceiver 可以启动Activity 作为响应,或者通过 NotificationMananger 提醒用户,或者启动 Service 等等。


生命周期只有十秒左右,如果在 onReceive() 内做超过十秒内的事情,就会报错  ANR(Application No

Response) 的对话框。

   (5)分类

        1)普通广播

sendBroadcast(intent2);

            发送一个广播,所以监听该广播的广播接收者都可以监听到改广播。


        2)异步广播

sendStickyBroadcast()

              当处理完之后的Intent ,依然存在,这时候registerReceiver(BroadcastReceiver, IntentFilter) 还能收到他的          值,直到你把它去掉 , 不能将处理结果传给下一个接收者 , 无法终止广播 .


        3)有序广播

sendOrderBroadcast()

1000 之间 , 值越         大 , 优先级越高 . 可以终止广播意图的继续传播 . 接收者可以篡改内容 .

4.生命周期

   生命周期只有十秒左右,如果在 onReceive() 内做超过十秒内的事情,就会报错 ANR(Application No

Response) 的对话框。

    调用对象  -     实现 onReceive() 方法   -   销毁


5. 静态实现

    (1)继承BroadcastReceiver ,实现 onReceive()方法 

package com.example.broadcast;

import com.example.boradcastdemo.ContentActivity;
import com.example.boradcastdemo.MainActivity;
import com.example.notification.Ynotification;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.BoringLayout;
import android.util.Log;


public class Tbroadcast extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
//获取通知
String msg=intent.getStringExtra("data");

Log.i("MSG","Tbroadcast: "+ msg);


//设置通知
Ynotification ynotification=new Ynotification(context);
Intent intent2= new Intent(context,ContentActivity.class);
intent2.putExtra("data",msg);
ynotification.sendNotification("Receive","Tbroadcast",msg,intent2);


}

}

     (2)AndroidMianfest.xml 中注册 

<!-- 注册 Receiver -->
<receiver
android:name="com.example.broadcast.Tbroadcast"
>
<intent-filter
android:priority="200"
>
<action android:name="BCD_DEMO"/>
</intent-filter>

</receiver>

6.动态实现

   (1)和静态实现 第一步一样

   (2)代码实现

//动态注册
IntentFilter filter=new IntentFilter();
filter.addAction("BCD_DEMO");
Mbroadcast mbroadcast=new Mbroadcast();
registerReceiver(mbroadcast, filter);
//发送广播
Intent intent1 = new Intent(MainActivity.this,Mbroadcast.class);
intent1.putExtra("data",editText1.getText().toString());
sendBroadcast(intent1);

7.实例

   通过 发送 广播 ,并显示在 通知栏 ;

 (1)实现 Notification 类封装

package com.example.notification;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.example.boradcastdemo.R;

@SuppressLint("NewApi")
public class Ynotification {

private final int NOTIFICATION_ID = 1;

private Context Mcontext;
private NotificationManager manager;

// 初始化
public Ynotification(Context context) {
this.Mcontext = context;
manager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
}

// 发送 通知
public void sendNotification(String ticker, String title, String msg,
Intent intent) {

Builder builder = new Notification.Builder(Mcontext);
// 提醒图标和文字
builder.setTicker(ticker);
builder.setSmallIcon(R.drawable.ic_launcher);

// 通知栏 显示标题和信息
builder.setContentTitle(title);
builder.setContentInfo(msg);

// 设置 震动 ,声音 ,提示灯 (添加权限)
builder.setDefaults(Notification.DEFAULT_ALL);

// 设置 通知 点击后的跳转的页面
PendingIntent pIntent = PendingIntent.getActivity(Mcontext, 0, intent,
0);
builder.setContentIntent(pIntent);

// 建立通知
Notification notification = builder.build();
manager.notify(NOTIFICATION_ID, notification);
}

}


 (2)实现 TBroadcastReceive 接收

package com.example.broadcast;

import com.example.boradcastdemo.ContentActivity;
import com.example.boradcastdemo.MainActivity;
import com.example.notification.Ynotification;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.BoringLayout;
import android.util.Log;


public class Tbroadcast extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
//获取通知
String msg=intent.getStringExtra("data");

Log.i("MSG","Tbroadcast: "+ msg);


//设置通知
Ynotification ynotification=new Ynotification(context);
Intent intent2= new Intent(context,ContentActivity.class);
intent2.putExtra("data",msg);
ynotification.sendNotification("Receive","Tbroadcast",msg,intent2);


}

}


 (3)实现业务代码

// 静态注册 测试 Receiver
Intent intent = new Intent(MainActivity.this,Tbroadcast.class);
intent.putExtra("data",editText1.getText().toString());
intent.setAction("BCD_DEMO");
sendBroadcast(intent);

  (4)效果图

                               

Android- BroadCastReceiver 基础学习_broadcastreceiver