Android中广播(BroadcastReceiver)的详细讲解.
1. BroadcastReceiver的注册过程:
(1).广播消息发出来后,只有订阅了该广播的对象才会接收发出来的广播消息并做出相应处理。
**(2).**Android广播分为两个方面:广播发送者和广播接收者。Android中的广播使用了观察者模式,基于消息的发布/订阅事件模型。广播接收者通过Binder机制向AMS进行注册,AMS查找符合相应条件的Broadcastreceiver,将广播发送到BroadcastReceiver(一般情况下是Activity)相应的消息循环队列中。消息循环执行拿到此广播,回调BroadcastReceiver中的onReceive()方法。
(3).广播发送者和广播接收者的执行顺序是异步的,发出去的广播不会关心有无接收者的接收,也不确定接收者是什么时候能接收到。
2. BroadcastReceiver的步骤:
(1).注册广播:广播接收者在AMS中注册广播。
(2).发送广播:广播发送者向AMS中发送广播。
(3).接收广播:广播接收者拿到广播后调用onReceive()方法执行。
以下是一个发送短信的例子,代码如下:
BroadcastReceiverHelper 类:
package com.scd.broadcastreceiver.helper;
import com.scd.broadcastreceiver.activity.MainActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BroadcastReceiverHelper extends BroadcastReceiver {
private MainActivity mActivity = null;
public BroadcastReceiverHelper(MainActivity mContext) {
super();
mActivity = mContext;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MainActivity.ACTION_SENDMESSAGE)) {
toast("发送成功");
} else if (intent.getAction()
.equals(MainActivity.ACTION_DELIVERMESSAGE)) {
toast("接收成功");
}
}
public void toast(String text) {
Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
}
}
MainActivity类:
package com.scd.broadcastreceiver.activity;
import com.scd.broadcastreceiver.R;
import com.scd.broadcastreceiver.helper.BroadcastReceiverHelper;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button mButton = null;
public static String ACTION_SENDMESSAGE = "com.scd.broadcastreceiver.MainActivity.SendMessage";
public static String ACTION_DELIVERMESSAGE = "com.scd.broadcastreceiver.MainActivity.DeliverMessage";
private BroadcastReceiverHelper mHelperS = null;
private BroadcastReceiverHelper mHelperD = null;
private SmsManager mSmsManager = null;
private String mTelephone = "13607567010";
private String mContext = "你好,有什么需要帮助的吗?";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.sendBroadcast);
mButton.setOnClickListener(this);
mSmsManager = SmsManager.getDefault();
// 注册广播
registerBroadcast();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sendBroadcast: {
//发送广播
sendBroadcastReceiver(mTelephone, mContext);
break;
}
default:
break;
}
}
public void registerBroadcast() {
// 发送
IntentFilter intentFilterS = new IntentFilter(ACTION_SENDMESSAGE);
mHelperS = new BroadcastReceiverHelper(MainActivity.this);
this.registerReceiver(mHelperS, intentFilterS);
// 接收
IntentFilter intentFilterD = new IntentFilter(ACTION_DELIVERMESSAGE);
mHelperD = new BroadcastReceiverHelper(MainActivity.this);
this.registerReceiver(mHelperD, intentFilterD);
}
public void sendBroadcastReceiver(String telephone, String content) {
if (telephone != null) {
Intent sIntent = new Intent(ACTION_SENDMESSAGE);
// 短信成功发送后才发送该广播
PendingIntent sentIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, sIntent, 0);
Intent dIntent = new Intent(ACTION_DELIVERMESSAGE);
// 短信接收后才发送该广播
PendingIntent deliveryIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, dIntent, 0);
mSmsManager.sendTextMessage(telephone, null, content, sentIntent,
deliveryIntent);
// this.sendBroadcast(intent);
}
}
}
注意:需要添加以下权限:
<!-- 短信权限 -->
<uses-permission android:name="android.permission.SEND_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
您可能感兴趣的文章:Android中BroadcastReceiver(异步接收广播Intent)的使用Android 广播大全 Intent Action 事件详解Android 监听apk安装替换卸载广播的实现代码Android中的广播和广播接收器代码实例android连接wifi时获取广播地址代码Android基于广播事件机制实现简单定时提醒功能代码Android广播接实现监听电话状态(电话的状态,拦截)Android 开机广播的使用及配置android如何默认打开小区广播具体实现Android学习之本地广播使用方法详解