广播之有序广播以及系统广播的短息

有序广播 

sendOrderedBroadcast(intent,null);

intentFilter.setPriority(988);//设置优先级
短信 

public class MySms extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())){
            Log.i("test","收到短信了");
            Bundle bundle=intent.getExtras();
           Object object[]= (Object[]) bundle.get("pdus");
            SmsMessage smsMessage[]=new SmsMessage[object.length];
            for (int i = 0; i < object.length; i++) {
                SmsMessage.createFromPdu((byte[])object[i]);
            }
            for (SmsMessage message : smsMessage) {
               String address= message.getOriginatingAddress();
               String body= message.getDisplayMessageBody();
                Log.i("test",address+","+body);
            }
        }
    }
需要配置权限 


<uses-permission android:name="android.permission.RECEIVE_SMS">uses-permission>这句是接受的权限
 
  
xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.g160628_android_y222_broadcastreceiver2">
    <uses-permission android:name="android.permission.RECEIVE_SMS">uses-permission>
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>
        activity>
        <receiver android:name=".MySms">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED">action>
            intent-filter>
        receiver>
    application>
manifest>

你可能感兴趣的:(广播之有序广播以及系统广播的短息)