Android实训案例(六)——四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听

Android中四大组件的使用时重中之重,我这个阶段也不奢望能把他所有的原理搞懂,但是最起码的,我要把他的各种使用方法了如指掌才行

BroadcastReceiver

接收系统的广播,比如电话,短信之类的

1.IP拨号器

我们在拨打电话的时候,我们系统也会事先发送一个广播,所以我们可以用广播接收者来接收到这个广播拨打电话的时候在电话号码前面加上一些优惠的长途短号,其逻辑就是入门使用广播的一个小案例,那我们新建一个IPCall项目

Android实训案例(六)——四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听_第1张图片

我们拨打电话的时候系统发一个广播,我们接受到这个广播拿到号码修改之后再放回去,就达到了我们IP拨号的目的,我们需要新建一个Receiver

CallReceiver

package com.lgl.ipcall;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;/** * 电话广播 * Created by lgl on 16/4/10. */public class CallReceiver extends BroadcastReceiver {    //接收到广播时调用    @Override    public void onReceive(Context context, Intent intent) {        Log.i("CallReceiver", "打电话");    }}

这个类暂时不需要做什么。最重要的还是注册,作为四大组建,他是需要在清单文件里注册的

 <!--注册广播-->        <receiver android:name=".CallReceiver">            <intent-filter>                <!--定义接收的广播-->                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />            </intent-filter>        </receiver>

别忘了,你监听了用户的隐私,是需要权限的

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

我们先运行一下,打个电话

这里写图片描述

好的,广播我们已经抓住了,那我们可以首先获取打出去的号码

String iphone = getResultData();Log.i("CallReceiver", "电话号码:"+iphone);

这样我们就可以拿到String类型的电话号码了

这里写图片描述

我们现在可以修改号码重新设置IP段了

        //添加IP段        String newPhone = "+86" + iphone;        //把修改后的号码放回去        setResultData(newPhone);

这样我们每次打电话他都会自动帮我们添加一个字段了

完整代码

package com.lgl.ipcall;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;/** * 电话广播 * Created by lgl on 16/4/10. */public class CallReceiver extends BroadcastReceiver {    //接收到广播时调用    @Override    public void onReceive(Context context, Intent intent) {        Log.i("CallReceiver", "打电话");        String iphone = getResultData();        Log.i("CallReceiver", "电话号码:" + iphone);        //添加IP段        String newPhone = "+86" + iphone;        //把修改后的号码放回去        setResultData(newPhone);    }}

2.短信拦截器

系统受到了一条短信后,也是会发一条广播的,所有我们可以在中间写一个广播接受者去进行我们的操作,这里,我们继续在IP拨号器这个项目中写吧,就不新建项目了,不然等下上传也麻烦

我们新建一个Class——SMSReceiver,重写他的onReceive方法,然后我们先注册

<!--注册广播-->        <receiver android:name=".SMSReceiver">            <intent-filter>                <!--定义接收的广播,被Google隐藏的权限操作-->                <action android:name="android.provider.Telephony.SMS_RECEIVED" />            </intent-filter>        </receiver>

切記,权限

<uses-permission android:name="android.permission.RECEIVE_SMS" />

SMSReceiver

package com.lgl.ipcall;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.telephony.SmsMessage;import android.util.Log;/** * 短信拦截器 * Created by lgl on 16/4/10. */public class SMSReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        System.out.println("收到短信");        //获取短信内容        Bundle bundle = intent.getExtras();        //返回的是一个Object数组        Object[] objects = (Object[]) bundle.get("pdus");        //遍历数组得到短信内容        for (Object object : objects) {            //把数组元素转换成短信对象            SmsMessage sms = SmsMessage.createFromPdu((byte[]) object);            //获取发件人号码            String toPhone = sms.getOriginatingAddress();            //获取短信内容            String smsContent = sms.getMessageBody();            Log.i("SMSReceiver", "发件人号码:" + toPhone + "短信内容" + smsContent);            //判断是否是拦截的号码            if (toPhone.equals("12345678910")) {                //拦截广播                abortBroadcast();            }        }    }}

这样我们运行之下,你会发现,虽然走到拦截这一步,但是并没有阻止显示在短信收件箱里,这里,我们要注意一个优势,就是广播接收者是有优先级定义的,我们只需要在清单注册根节点的intent-filter标签里定义一个

android:priority="1000"

官方文档是说数值在-1000到1000之间,但是最高支持int的最大值的权限,int最大值是多少?自己去看API

3.监听SD卡

我们在对SD卡进行读写的时候会用到,其实也就是巩固一下对广播的使用,做那种语音助手之类的辅助软件,广播和服务还是很有用的,我们还是定义一个SDReceiver并且在清单文件注册

 <!--注册广播-->        <receiver android:name=".SDReceiver">            <intent-filter>                <!--SD卡就绪广播-->                <action android:name="android.intent.action.MEDIA_MOUNTED" />                <!--SD卡拔出广播-->                <action android:name="android.intent.action.MEDIA_REMOVED" />                <!--SD卡卸载广播-->                <action android:name="android.intent.action.MEDIA_UNMOUNTABLE" />                <data android:scheme="file"/>            </intent-filter>        </receiver>

我们现在可以监听了

SDReceiver

package com.lgl.ipcall;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;/** * 监听SD卡 * Created by lgl on 16/4/10. */public class SDReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        //判断广播        String action = intent.getAction();        if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {            Log.i("SDReceiver", "SD卡就绪");        } else if (action.equals(Intent.ACTION_MEDIA_REMOVED)) {            Log.i("SDReceiver", "SD卡拔出");        } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTABLE)) {            Log.i("SDReceiver", "SD卡卸载");        }    }}

4.流氓软件

我们监听到开机就启动这个软件,而且不让其退出,达到流氓的效果

Android实训案例(六)——四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听_第2张图片

先不让他退出,我们在MainActivity中

    @Override    public void onBackPressed() {        //禁止返回键        // super.onBackPressed();    }

只要你一安装,就退出不了了,我们再设置一下开机启动,写一个监听器动的广播罢了,我们新建一个RebootReceiver,先注册吧

<!--注册广播-->        <receiver android:name=".RebootReceiver">            <intent-filter>                <!--定义接收的广播-->                <action android:name="android.intent.action.BOOT_COMPLETED" />            </intent-filter>        </receiver>

这个也是需要权限的

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

RebootReceiver

package com.lgl.ipcall;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;/** * 开机启动 * Created by lgl on 16/4/10. */public class RebootReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        Log.i("RebootReceiver", "开机");        //启动        Intent i = new Intent(context, MainActivity.class);        //在Activity之外启动需要设置Flags        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        context.startActivity(i);    }}

5.应用安装卸载监听

我们如果做手机助手或者应用的时候或许可以用得上这玩意,新建一个APPReceiver,然后去注册

<!--注册广播-->        <receiver android:name=".APPReceiver">            <intent-filter>                <!--安装应用-->                <action android:name="android.intent.action.PACKAGE_ADDED" />                <!--更新应用-->                <action android:name="android.intent.action.PACKAGE_REPLACED" />                <!--卸载应用-->                <action android:name="android.intent.action.PACKAGE_REMOVED" />                <!--携带包名-->                <data android:scheme="package"/>            </intent-filter>        </receiver>

然后我们来判断

package com.lgl.ipcall;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.util.Log;/** * Created by lgl on 16/4/10. */public class APPReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        //判断广播类型        String action = intent.getAction();        //获取包名        Uri appName = intent.getData();        if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {            Log.i("APPReceiver", "安装" + appName);        } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {            Log.i("APPReceiver", "更新" + appName);        } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {            Log.i("APPReceiver", "卸载" + appName);        }    }}

这样我们完全就可以监听状态了,我们以下载一个豌豆荚为例

这里写图片描述

6.自定义广播

这里,我们有特定需求的时候会用,我们先定义一个Button

         <Button            android:id="@+id/btn_myreceiver"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="发送自定义广播" />

他的点击事件就是发送一个广播,我这里自定义一个广播名字liuguilin

             case R.id.btn_myreceiver:                Intent i = new Intent();                i.setAction("liuguilin");                sendBroadcast(i);                break;

然后我们新建一个MyReceiver,注册

        <receiver android:name=".MyReceiver">            <intent-filter>                <!--自定义广播-->                <action android:name="liuguilin" />            </intent-filter>        </receiver>

我们写个打印语句

这里写图片描述

7.有序广播和无序广播

这两种广播的区别

  • 有序广播:接收这条广播是按优先级来的
  • 无序广播:无条件直接接收

-1.发送有序广播

我们定义一个Button

         <Button            android:id="@+id/btn_haveorder"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="发送有序广播" />

然后发送一个广播

case R.id.btn_haveorder:                Intent intent = new Intent();                intent.setAction("com.lgl.good");                //有序广播并且携带数据                sendOrderedBroadcast(intent, null, null, null, 0, "自定义广播内容", null);                break;

这里我定义了三个广播,然后他们的优先级分辨是1000,600,300

        <!--有序广播-->        <receiver android:name=".OrderReceiver">            <intent-filter android:priority="1000">                <action android:name="com.lgl.good"/>            </intent-filter>        </receiver>        <!--有序广播-->        <receiver android:name=".OrderReceiverTwo">            <intent-filter android:priority="600">                <action android:name="com.lgl.good"/>            </intent-filter>        </receiver>        <!--有序广播-->        <receiver android:name=".OrderReceiverThree">            <intent-filter android:priority="300">                <action android:name="com.lgl.good"/>            </intent-filter>        </receiver>

最后运行的结果

这里写图片描述

Demo下载:

更多相关文章

  1. Android之Toast自定义管理
  2. Android 自定义阴影
  3. android app --- 快速集成SharaSDK ,SMS实现短信验证(一)2.0版之前

随机推荐

  1. EasyPermission Android(安卓)6.0 最简洁
  2. 学习笔记:Android中使用网络技术的方法(web
  3. 【Arduino】Arduino接收字符串
  4. Android(安卓)常用git命令
  5. iTextPdf最简单最彻底解决中文显示
  6. 我的Android进阶之旅------>Android实现
  7. Android屏幕图标尺寸规范
  8. android.support.v7.widget.TintContextW
  9. Android(安卓)SQLite 支持嵌套事务吗?
  10. android web services2