//将广播的对象创建出来 广播的对象须继承extends BroadcastReceiver
abortBroadcast();//中断广播
MyBoradCast2 boradCast2 =new MyBoradCast2();
//创建一个意图过滤器
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("www.qq.weixin");
//动态代码注册广播
registerReceiver(boradCast1, intentFilter);
registerReceiver(boradCast2, intentFilter);
public void Send(View v){
Intent intent = new Intent();
intent.setAction("www.qq.weixin");
intent.putExtra("str", "zhangsan");
sendBroadcast(intent);
}
//取消广播的注册
unregisterReceiver(boradCast1);
unregisterReceiver(boradCast2);
<!-- 允许接受拨打电话的 权限-->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<!-- 系统拨打电话的 action-->
<receiver android:name="com.qianfeng.day21_broadcast4.CallLogBroadCast">
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
//Bundle bundle = intent.getExtras();
//电话号码 String num = bundle.getString(Intent.EXTRA_PHONE_NUMBER);
<!-- 电话状态的权限 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<receiver android:name="comqianfeng.day21_broadcast5.MyPhoneBroadCast">
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
//电话管理者对象
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//得到电话状态
int state = manager.getCallState();
Bundle bundle = intent.getExtras();
String num = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);//获取当前拨打电话的号码
switch (state) {
case TelephonyManager.CALL_STATE_IDLE://挂断的状态
System.out.println("挂断");
break;
case TelephonyManager.CALL_STATE_OFFHOOK://接听的状态
System.out.println("接听");
break;
case TelephonyManager.CALL_STATE_RINGING://响铃的状态
System.out.println("响铃"+num);
break;
<!-- 添加电量的状态的权限-->
<uses-permission android:name="android.permission.BATTERY_STATS"/>
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(myBatteryBroadCast, intentFilter);
if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())){
//获取电量的等级
int level = intent.getIntExtra("level", 0);
//获取总的电量
int scale = intent.getIntExtra("scale", 0);
//当前电量 40%
int current = (level*100)/scale;
<!-- 检测网络状态的权限-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
/**
* 检测网络变化的
*/
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(myNetWorkBroadCast, intentFilter);
// /得到网络管理者对象
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info == null) {
Toast.makeText(context, "网络异常", Toast.LENGTH_SHORT).show();
} else {
if (info.isConnected()) {// 判断网络是否连接
Toast.makeText(context, "网络正常", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "网络没连接", Toast.LENGTH_SHORT).show();
}
}
<!-- 允许接受短信的权限-->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
</activity>
<!-- 接收 短信 广播接收器 -->
<receiver android:name="com.qianfeng.day21_broadcast8.MySmsBroadCast">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
Bundle bundle = intent.getExtras();
Object[] pduses= (Object[]) bundle.get("pdus");
for(Object pdus:pduses){
byte[] pdu = (byte[]) pdus;
SmsMessage smsMessage = SmsMessage.createFromPdu(pdu);
String num = smsMessage.getOriginatingAddress();//得到发送短信的号码
String body = smsMessage.getDisplayMessageBody();//得到短信的内容
long time = smsMessage.getTimestampMillis();//得到发送短信的时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String t = simpleDateFormat.format(new Date(time));