屏幕适配加广播

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
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卡挂在状态
···
//监听SD卡挂载状态的广播
public class SDRecevier extends BroadcastReceiver{
@Override
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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值