BroadcastReceiver广播接收者基础总结

来源:互联网 发布:竞业限制补偿金算法 编辑:程序博客网 时间:2024/06/05 00:35

一,发送一个广播

public class SendBroadcast extends FragmentActivity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_sendbroadcast);    }    /**     * 发送广播的单击事件:     * 发送广播其实就是定义一个意图,在意图中指定广播的action     * 然后以广播的形式发送出去     */    public void sendBroadcast(View view){        Intent intent = new Intent();        intent.setAction("com.yin.sendbroadcast");        sendBroadcast(intent);    }}

二,接收刚才发送的广播

<receiver android:name="com.mycompany.mysimple.myBroadcastReceiver.MyBroadcast">            <!--添加意图过滤器指定我定义的广播的action-->            <intent-filter>                <action android:name="com.yin.sendbroadcast"/>            </intent-filter>        </receiver>


/** * Created by Administrator on 2015/8/20. * * 广播接收者所在的应用如果原来没有被启动,或者说进程没有被启动,当收到广播时默认会启动 * 该应用的进程,可通过观察DDMS来验证。 */public class MyBroadcast extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        String action = intent.getAction();        Toast.makeText(context,"广播的action:"+action,Toast.LENGTH_SHORT).show();    }}

三,外拨电话的广播接收者

<receiver android:name="com.mycompany.mysimple.myBroadcastReceiver.OutCallReceiver">            <intent-filter>                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>            </intent-filter>        </receiver>

/** * Created by Administrator on 2015/8/17. * BroadCastReceiver是Android四大组件之一,需要在清单文件中注册(若涉及到权限需添加权限); * 广播接收者用于接收某个指定电台的广播,这里是接收外拨电话(管理外拨电话的电台)的广播; * 一旦有外拨电话的请求,电台就会发送广播,相应的广播接收者就会收到该广播; * 广播接收者一旦部署到手机上,及时其所在app没有被打开,即使你杀死了app的进程,一旦所监控的广播发出 * 广播,该广播接收者仍然会收到,做相应的处理 */public class OutCallReceiver extends BroadcastReceiver {    private String resultData;    @Override    public void onReceive(Context context, Intent intent) {        resultData = getResultData();//可能是null,不区分有序无序,但是无序时虽然不报错但是返回的时null没什么意义        String newNum = "9527"+resultData;        setResultData(newNum);//只有有序广播起作用,无序广播无效        Toast.makeText(context,newNum,Toast.LENGTH_SHORT).show();//        abortBroadcast();只有有序广播有效,无序广播无效    }}


四,SDcard状态的广播接收者

<!--其中scheme="file"是监控sd卡、usb等的固定写发,不加这个是不行的-->        <receiver android:name="com.mycompany.mysimple.myBroadcastReceiver.ListenSDCard">            <intent-filter>                <action android:name="android.intent.action.MEDIA_MOUNTED"/>                <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>                <action android:name="android.intent.action.MEDIA_REMOVED"/>                <data android:scheme="file"/>            </intent-filter>        </receiver>

/** * Created by Administrator on 2015/8/17. * 监控SDCard的状态:安装,解除安装,移除等; */public class ListenSDCard extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        String resultData = getResultData();//在这里结果是null        String action = intent.getAction();        Uri data = intent.getData();        String dataString = intent.getDataString();        if(resultData == null){            Log.i("ReceiverFlag","null,"+action+","+data.toString()+","+dataString);        }else {            Log.i("ReceiverFlagg",resultData);        }//        Toast.makeText(context,resultData,Toast.LENGTH_SHORT).show();    }}

五,短信的广播接收者

<receiver android:name="com.mycompany.mysimple.myBroadcastReceiver.SmsReceiver">            <intent-filter android:priority="1000">                <!-- android.provider.Telephony.SMS_RECEIVED这个action在4.2版本后就不 自动提示了 -->                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>            </intent-filter>        </receiver>

/**短信广播接收者,当监测到有短信发来是,会收到信息 * Created by Administrator on 2015/8/17. */public class SmsReceiver extends BroadcastReceiver {    /**     * 监控短信的功能是谷歌逐渐要取消的,实际上Android4.2以后,短信广播相关的API你用eclipse时,已经     * 不给你提示了,但是你写出的代码还是可以识别的,估计4.4以后就完全不能用了,一下这段代码是固定写法,     * 记住就行了     */    @Override    public void onReceive(Context context, Intent intent) {        Log.i("有短信", "有短信来了");        Object[] objs = (Object[])intent.getExtras().get("pdus");        Log.i("ObjsSize", String.valueOf(objs.length));        for(Object obj:objs){            SmsMessage sms= SmsMessage.createFromPdu((byte[]) obj);            String body = sms.getMessageBody();            String address = sms.getOriginatingAddress();            Log.i("body", body);            Log.i("address", address);            //终止广播//abortBroadcast();        }    }}


0 0