无法处理收到的短信

时间:2015-05-07 08:39:32

标签: java android sms broadcastreceiver

我想使用BroadcastReceiver处理收到的短信。如果新短信到达,将显示新活动。这是我的努力:

public class SMSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        StringBuilder sb = new StringBuilder();
        if (bundle != null) {           
            Object[] pdus = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdus.length; i++) {
                //yes, I need PDU
                sb.append(pdus[i]);
            }
        }

        Intent result = new Intent(context, MainActivity.class);
        result.putExtra(Mapping.PDU_TEXT, sb.toString());
        result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(result);

    }

活动:

public class MainActivity extends Activity {

    private TextView mPDUTextView;

    private String pduText;

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);

        savedInstanceState.putString(Mapping.PDU_TEXT, pduText);

    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        pduText = savedInstanceState.getString(Mapping.PDU_TEXT);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            if (getIntent().getExtras() != null) {
                pduText = this.getIntent().getExtras()
                        .getString(Mapping.PDU_TEXT);
            }
        }
        mPDUTextView = (TextView) this.findViewById(R.id.pduTextView);
        mPDUTextView.setText(pduText);
    }

}

另请查看清单:

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

         <receiver android:name="ru.tenet.pdureceiver.SMSReceiver" >
            <intent-filter>             
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
                <data android:scheme="sms" />
                <data android:port="8998" />
            </intent-filter>
        </receiver>   


        <activity
            android:name="ru.tenet.pdureceiver.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>
    </application>

我已在真实设备上安装了我的应用。如果我发送消息,则不会运行任何活动。怎么了?

1 个答案:

答案 0 :(得分:2)

试试这个:

<intent-filter android:priority="1" >
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>