BroadCastReceiver简介
BroadCastReceiver源码位于:framework/base/core/java/android.content.BroadcastReceiver.java
广播接收者(BroadcastReceiver)用于接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、Context.sendOrderedBroadcast()来实现的。通常一个广播Intent可以被订阅了此Intent的多个广播接收者所接收。
广播是一种广泛运用的在应用程序之间传输信息的机制。而BroadcastReceiver是对发送出来的广播进行过滤接收并响应的一类组件;
来自普通应用程序,如一个应用程序通知其他应用程序某些数据已经下载完毕。
BroadcastReceiver自身并不实现图形用户界面,但是当它收到某个通知后,BroadcastReceiver可以启动Activity作为响应,或者通过NotificationMananger提醒用户,或者启动Service等等。
机制
在 Android 里面有各种各样的广播,比如电池的使用状态,电话的接收和短信的接收都会产生一个广播,应用程序开发者也可以监听这些广播并做出程序逻辑的处理。如图:
代码案例:
消息发送与接收(启动一个服务,广播发送内容,页面接收广播发送的内容在页面显示):
图片看不明白就看代码吧!有注释:
注册有两种方法:
第一种:手动注册,在类中重写onResume和onPause方法
第二种:在AndroidManifest.xml中注册添加
这里第一种和第二种方法都用了的。
activity_main.xml中代码(定义一个文本框和一个按钮):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动一个服务" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java中代码:
package com.mcy.broadcastreceiver;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private static final String ACTION = "com.mcy";
private Button button;
private TextView text;
private BroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
text = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(this);
//创建广播接收器
receiver = new BroadcastReceiver() {
//重写onReceive()方法来接收以Intent对象为参数的消息。
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String str = bundle.getString("jb");
text.setText(str);
}
};
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, BroadcastService.class);
//启动服务
startService(intent);
}
//注册,手动注册
@Override
protected void onResume() {
//过滤
IntentFilter filter = new IntentFilter(ACTION);
registerReceiver(receiver, filter);
super.onResume();
}
@Override
protected void onPause() {
//注销
unregisterReceiver(receiver);
super.onPause();
}
}
在新建一个BroadcastService.java类,其代码:
package com.mcy.broadcastreceiver;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class BroadcastService extends IntentService {
private static final String TAG = "BroadcastReceiver";
private static final String ACTION = "com.mcy";
public BroadcastService() {
super("BroadcastReceiver");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "服务开始运行。。。");
//循环发送广播
for(int i = 0; i < 100; i++){
Intent intent1 = new Intent();
intent1.setAction(ACTION);
//发送广播内容
intent1.putExtra("jb", "服务进度:"+i+"%");
//发送广播
sendBroadcast(intent1);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//最后一次发送,循环结束
Log.i(TAG, "服务结束");
Intent intent2 = new Intent();
intent2.setAction(ACTION);
intent2.putExtra("jb", "服务完成");
sendBroadcast(intent2);
}
}