BroadcastReceiver 广播接收者
注册
清单文件中注册:
<application 层中
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<receiver android:name="MyBroadcastReceiver">
<intent-filter
android:priority="59520" >
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.bz.mydisorderreceiver"/>
<action android:name="com.bz.oreceiver"/>
<data android:scheme="file"/>
<data android:scheme="package"/>
</intent-filter>
在代码中注册:(与在清单文件注册的区别:代码注册-只有在程序运行时才能接收广播)
ScreenReceiver screenreceiver;
public void screenReceiver(){
screenreceiver = new ScreenReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.SCREEN_OFF");
filter.addAction("android.intent.action.SCREEN_ON");
registerReceiver(screenreceiver, filter);
}
@Override
protected void onDestroy() {
unregisterReceiver(screenreceiver);
super.onDestroy();
}
第一次写的广播 MyBroadcastReceiver.java
package com.bz.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if("android.intent.action.MEDIA_MOUNTED".equals(action)){
System.out.println("SD卡被挂载");
}
else if("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
System.out.println("SD卡被卸载");
}
else if("android.intent.action.NEW_OUTGOING_CALL".equals(action)){
System.out.println("拨打电话");
String currentnum = getResultData();
if(currentnum.startsWith("0"))
setResultData("17951"+currentnum);
}
else if("android.provider.Telephony.SMS_RECEIVED".equals(action)){
System.out.println("收到短信");
Object objects[] = (Object[]) intent.getExtras().get("pdus");
for (Object obj : objects) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
String address = smsMessage.getOriginatingAddress();
String messagebody = smsMessage.getMessageBody();
System.out.println("收到短信 "+address+":"+messagebody);
}
}
else if("android.intent.action.PACKAGE_ADDED".equals(action)){
System.out.println("安装应用:" + intent.getData());
}
else if("android.intent.action.PACKAGE_REMOVED".equals(action)){
System.out.println("卸载应用:" + intent.getData());
}
else if("android.intent.action.BOOT_COMPLETED".equals(action)){
System.out.println("重启手机调用");
Intent intent1 = new Intent(context, MyService.class);
context.startService(intent1);
Intent intent2 = new Intent(context,MainActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
else if("com.bz.mydisorderreceiver".equals(action)){
Toast.makeText(context, intent.getStringExtra("name"), 1).show();
}
else if("com.bz.oreceiver".equals(action)){
Toast.makeText(context, getResultData(), 1).show();
setResultData("什么都没有");
}
}
}
动态注册的 ScreenReceiver.java
package com.bz.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.intent.action.SCREEN_OFF")) System.out.println("屏幕关闭了");
if(action.equals("android.intent.action.SCREEN_ON")) System.out.println("屏幕开启了");
}
}
发广播
普通广播
public void DisorderReceiver(View v){
Intent intent = new Intent();
intent.setAction("com.bz.mydisorderreceiver");
intent.putExtra("name", "每天晚上准时KEEP呀~");
sendBroadcast(intent);
}
有序广播
public void DynamicReceiver(View v){
Intent intent = new Intent();
intent.setAction("com.bz.oreceiver");
sendOrderedBroadcast(intent, null, new FinalReceiver(), null, 1, "根据黑马视频教程制作", null);
}