Android -- 怎么发出和接收广播, Broadcast, 电话拨号拦截,短信拦截

1. 发送广播

使用以下三个API可以发送广播

public void click(View view){
		Intent intent = new Intent();
		intent.setAction("com.itheima.xxxooo");
		//把这个自定义的广播发送出去
		//sendBroadcast(intent); //发送一条无序的广播事件
		//如果广播事件是无序发送出去的 所有的广播接受者 都会接受到这个事件
		
		//如果广播是有序的发送出去的, 广播接收者会按照优先级 接受到广播事件
		// 有序广播 特点: 高优先级的广播接受者 可以终止掉 广播事件
		//sendOrderedBroadcast(intent, null);
		
		//第三个参数设置, 无论此接受者的优先级有多低,或是其他中断了广播,他最后都能接收到
		sendOrderedBroadcast(intent, null, new FinalRecevier(), null, 0, null, null);
	}

2. 接收广播,继承BroadCastReceiver, 需要在xml权限中注册接收器, 并且有些广播需要添加权限

public class MyBroadcastReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		System.out.println("reveriver 1 接收到了广播");
		Toast.makeText(context, "检查到了 自定义的广播事件", 1).show();
	}	
}
AndroidManifest.xml 注册接受者  可以设置优先级  int, 值越大 优先级越高
<receiver android:name=".MyBroadcastReceiver" >
            <intent-filter android:priority="1000">
                <action android:name="com.kevin.xxxooo" >
                </action>
            </intent-filter>
        </receiver>
        <receiver android:name=".MyBroadcastReceiver2" >
            <intent-filter android:priority="1000">
                <action android:name="com.kevin.xxxooo" >
                </action>
            </intent-filter>
        </receiver>
        <receiver android:name=".MyBroadcastReceiver3" >
            <intent-filter android:priority="1000">
                <action android:name="com.kevin.xxxooo" >
                </action>
            </intent-filter>
        </receiver>

广播接收者的注册可以在xml文件中, 也可以在代码中注册(一般在 onCreate这样的开始创建的方法中注册) 

MyBroadcastReceiver  myReceiver = new MyBroadcastReceiver ();
IntentFilter filter = new IntentFilter();
filter.addAction("com.kevin.xxxooo");
registerReceiver(myReceiver, filter);


3. 示例:拦截电话拨号

public class OutCallReceiver extends BroadcastReceiver {

	//当有广播事件产生的时候 就会执行onrecevie方法
	@Override
	public void onReceive(Context context, Intent intent) {
		System.out.println("onreceiver 发现了新的外拨电话...");
		String number = getResultData();//外拨的电话号码
		System.out.println("number="+number);
		//替换掉这个号码
		
		SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		String ipnumber = sp.getString("ipnumber", "");
		String newnumber = ipnumber+number;
		//设置外拨的电话号码
		setResultData(newnumber);
		//不会生效的 广播终止不了 显示的指定了接受者
		abortBroadcast();		
	}
}

AndroidManifest.xml  需要设置权限 和 注册广播接收者

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.ipdail"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima.ipdail.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		<!-- 定义了一个广播接受者  new出来了一个收音机   ,设置action 就相当于设置了监听的频道 -->
        <receiver android:name=".OutCallReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

 

4.示例: 短信拦截

public class SmsReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		System.out.println("短信收到了...");

		Object[] pdus = (Object[]) intent.getExtras().get("pdus");
		for (Object pdu : pdus) {
			SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
			String body = smsMessage.getMessageBody();
			String sender = smsMessage.getOriginatingAddress();
			System.out.println("body:" + body);
			System.out.println("sender:" + sender);
			if ("5556".equals(sender)) {
				// 短信的拦截, 高优先级的receiver将短信拦截,低优先级的就接受不到了
				abortBroadcast();
				SmsManager smsManager = SmsManager.getDefault();
				smsManager.sendTextMessage(sender, null, "我已经喜欢上 xxx了 ,你去死吧", null, null);
			}

		}
	}
}

AndroidManifest.xml 权限配置

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.smslistener"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.Translucent" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".SmsReceiver" >
            <intent-filter android:priority="1000" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>




 

posted @ 2014-04-02 11:37  今晚打酱油_  阅读(604)  评论(0编辑  收藏  举报