xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="@dimen/x160"
android:layout_height="wrap_content"
android:text="姓名"
android:textSize="20sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:background="@drawable/img1"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img2"
/>
一、广播接收者概念(****BroadcastReceiver****)****&****为什么需要广播接收者
1、BroadcastReceiver用来接收sendBroadcast()方法发出来的广播,可以通过Intent传递数据,它是一个抽象类
2、广播特点:数据的传递方向是单向的,调到固定的频率
3、安卓的广播作用范围有限,只在当前手机里有效
4、系统把一些重要的操作,通过广播的形式通知给所有的应用
--------IP拨号器--------
广播接收器必须继承BroadcastReceiver 并添加未实现的方法。
在清单文件里注册:
···
<receiverandroid:name=".DiaReceiver">
<intent-filter >
<actionandroid:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
···
需要处理外拨电话权限:
<uses-permissionandroid:name="android.permission.PROCESS_OUTGOING_CALLS"/>
创建一个项目
写一个类继承BroadcastReceiver
******如**********
···
//Ip拨号器的广播接收者
public class DiaReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//获取电话号
String number = getResultData();
// Log.e("TAG", "打电话"+number);
//设置拨打电话号
setResultData("17951"+number);
}
}
···
//监听SD卡挂载状态的广播
public class SDRecevier extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("android.intent.action.MEDIA_MOUNTED".equals(action)) {
Log.e("TAG", "SDCard已安装");
}elseif("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
Log.e("TAG", "SDCard已卸载");
}
}
}
同样也需要在清单文件里注册
···
<receiverandroid:name=".SDRecevier">
<intent-filter >
<actionandroid:name="android.intent.action.MEDIA_MOUNTED"/>
<actionandroid:name="android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme="file"/>
</intent-filter>
</receiver>
··
public class SMS extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Object[]object=(Object[]) intent.getExtras().get("pdus");
for (Objectobj : object) {
//创建短信的消息对象
SmsMessage message=SmsMessage.createFromPdu((byte[]) obj);
//获取短信的发送 者
String from=message.getOriginatingAddress();
//获取消息的内容
String msgBody = message.getMessageBody();
Log.e("TAG","from:"+from+"msgBody:"+msgBody);
//获取验证码
//判断from是不是自己发的‘
if("122".equals(from)) {
//通知界面,把短信内容添加到EditText
Intent data = new Intent();
data.setAction("com.krr.getcode");
data.putExtra("code", msgBody);
context.sendBroadcast(data);//发送广播
} else {
}
}
}
}
···
在清单文件里注册---------->
···
<receiver android:name=".SMS">
<intent-filter >
<actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
![](https://i-blog.csdnimg.cn/blog_migrate/dc1899d434c52cf1af9304e9d779c119.jpeg)