1,创建一个类继承BroadcastReceiver
class SDCardStatusReceiver extends BroadcastReceiver {
}
2,在清单文件中的Application节点下进行配置
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
...
...
<receiver android:name="com.sdstatus.SDCardStatusReceiver" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme="file">data>
intent-filter>
receiver>
application>
3,在onreceive方法里面编写业务逻辑
public class SDCardStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "外部存储卡已被拔出,微信头像和朋友圈功能暂不可用", 0).show();
}
}
1,添加权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
2,在清单文件的application节点中进行配置
<receiver android:name="com.bootcomplete.BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">action>
intent-filter>
receiver>
1,添加权限
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
2,在清单文件的application节点中进行配置
<receiver android:name="com.ipdail.OutCallRecevier">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
intent-filter>
receiver>
3,获得外拨电话号,修改外拨电话号
public class OutCallRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sp = context.getSharedPreferences("config", 0);
String ipnumber = sp.getString("ipnumber", "");
//获取外拨电话号
String number = getResultData();
if (number.startsWith("0")) {
//设置外拨电话号
setResultData(ipnumber + number);
}
}
}
<receiver android:name="com.datacollection.AppStatusReceiver" >
<intent-filter >
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package">data>
intent-filter>
receiver>
1,添加权限
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
2,在清单文件的application节点中进行配置
<receiver android:name="com.smslistener.SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
intent-filter>
receiver>
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("短信到来了....");
//短信的数据是pdu的数据,必须对短信的格式很了解才可以解析短信.
Object[] objs = (Object[]) intent.getExtras().get("pdus");
for(Object obj:objs){
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
String body = smsMessage.getMessageBody();
String sender = smsMessage.getOriginatingAddress();
System.out.println("body:"+body);
System.out.println("sender:"+sender);
abortBroadcast();
}
}
}
自Android 3.1之后,所有新安装但未被执行过的apk,以及那些被用户强行停止的apk,都会处于stopped状态。这个状态下,apk中的广播接收者,均处于未激活状态, 无法履行监听功能。
在广播发送方发送广播时需要设置Intent.FLAGINCLUDESTOPPED_PACKAGES
Intent intent = new Intent();
intent.setAction("com.broadcast");
if (android.os.Build.VERSION.SDK_INT >= 12) {
intent.setFlags(32);//3.1以后的版本需要设置Intent.FLAG_INCLUDE_STOPPED_PACKAGES
}
sendBroadcast(intent);
1,发送
Intent intent = new Intent();
//自定义action
intent.setAction("com.ccav.XWLB");
//可以添加参数
intent.putExtra("key", "dfafadfa");
sendBroadcast(intent);
2,接收
<receiver android:name="com.myreceiver.MyReceiver">
<intent-filter >
//填写要接收自定义广播的action
<action android:name="com.ccav.XWLB"/>
intent-filter>
receiver>
有序广播
广播消息是按照一定的顺序传达的,高优先级的先得到广播消息,低优先级的后得到
高优先级的可以拦截广播消息或者修改广播消息
效率比较低
无序广播
广播消息没有顺序,同时接受广播消息
不可以修改广播消息
效率高
Intent intent = new Intent();
intent.setAction("com.gov.SENDMASHROOM");
// 发送有序广播
// intent 意图
// receiverPermission 权限 默认null
// resultReceiver 结果接受者 null
// scheduler 消息处理器 null 默认
// initialCode 初始化码
// initialData 初始化数据
// initialExtras null 额外的数据
sendOrderedBroadcast(intent, null, null, null, 1, "主席讲话: 每人10斤蘑菇", null);
1,优先级在哪里配置,范围是多少
<receiver android:name="com.smslistener.SmsReceiver">
//在此处配置,范围从1000到-1000
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
intent-filter>
receiver>
2,如何获取数据,如何修改数据
//获取数据
String data = getResultData();
//修改数据
setResultData("主席讲话:每人5斤蘑菇");
3,如何终止有序广播
abortBroadcast();
4,resultReceiver什么时候收到广播,需要在清单文件中配置吗
* 在所有广播接收者都收到消息后接受到广播
* 即使有广播接收者终止了广播,resultReceiver也可以接收到广播
* 不需要在清单文件中配置
5,如何动态注册广播接收者
receiver = new ScreenStatusReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.SCREEN_OFF");
filter.addAction("android.intent.action.SCREEN_ON");
registerReceiver(receiver, filter);
在android里面有一些产生非常频繁的广播事件,在清单文件里面配置是不会生效。电量变化、屏幕锁屏、解锁,这些广播事件只能利用代码注册
动态注册的广播接收者可以将其关闭掉
unregisterReceiver(receiver);