使用发送有序广播方法,广播优先级才能生效。

findViewById(R.id.button1).setOnClickListener(new OnClickListener()
{
                         
    @Override
    public void onClick(View v)
    {
        Intent intent = new Intent();
        intent.setAction("com.example.aex56");
        intent.putExtra("key", "value");
        sendOrderedBroadcast(intent, null);//发送有序广播
    }
});


XML文件设置广播优先级:

<receiver
    android:name="com.example.aex56_ordered_broadcast.MyReceiver1"
    android:enabled="true"
    android:exported="true" >
    <intent-filter android:priority="2500">
        <action android:name="com.example.aex56" />
    </intent-filter>
</receiver>
<receiver
    android:name="com.example.aex56_ordered_broadcast.MyReceiver2"
    android:enabled="true"
    android:exported="true" >
    <intent-filter android:priority="3000">
        <action android:name="com.example.aex56" />
    </intent-filter>
</receiver>




接收电话呼出广播,自动添加号码前缀呼出;设置短信接收者最高优先级,拦截短信。

XML权限及优先级设置:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.aex58_ordered_tel"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name="com.example.aex58_ordered_tel.MyTelReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.example.aex58_ordered_tel.MySmsReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="9999999999999999999999999999999">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>



电话呼出广播接收:

public class MyTelReceiver extends BroadcastReceiver
{
    //监测电话号码
    /*1.添加电话呼出权限
     * uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"
     *
     *2.接收系统电话呼出的广播
     * <intent-filter >
     * <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
     * </intent-filter>
     *
     * 其他系统广播:
     * android.intent.action.BOOT_COMPLETED  开机启动广播
     */
          
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String telnum = getResultData();//得到电话号码
        telnum = "0592" + telnum;
        setResultData(telnum);//设置拨出的电话号码
    }
}


短信接收者:

public class MySmsReceiver extends BroadcastReceiver
{
      
    /*
     * 拦截短信
     * 1.添加短信接收权限
     * uses-permission android:name="android.permission.RECEIVE_SMS"
     *
     * 2.设置广播优先级,接收系统收短信的广播
     * <intent-filter android:priority="99999999999999999999999999"> 设置广播优先级
     *     <action android:name="android.provider.Telephony.SMS_RECEIVED"/>接收系统收到短信时的广播
     * </intent-filter>
     *
     * 3.重写onReceive
     * abortBroadcast();
     */
      
      
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.e("msg", "mmm");
        abortBroadcast();//关闭广播
    }
}