Android BroadcastReceiver没有检测到短信

时间:2016-09-16 09:17:20

标签: java android

我不知道出了什么问题,但我在这个问题上遇到了困难。一旦收到消息,该程序应该显示一个祝酒词。我已经尝试在清单文件中添加优先级,但它不起作用。

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.comfy.textforwarder">

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="24" />

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

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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>
    <activity android:name=".Settings" />

    <receiver
        android:name=".MyReceiver"
        android:enabled="false"
        android:exported="true">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

广播接收器

public void onReceive(Context context, Intent intent) {
        Log.d("RECEIVE", "Receiving SMS");
        Bundle intentExtras = intent.getExtras();
        if(intentExtras != null){
            Object[] sms = (Object[]) intentExtras.get("pdus");
            String msgStr = "";
            for(int i = 0; i < sms.length; ++i){
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
                String smsBoddeh = smsMessage.getMessageBody().toString();
                String addr = smsMessage.getOriginatingAddress();

                msgStr += "SMS From " + addr + "\n";
                msgStr += smsBoddeh + "\n";
            }
            Toast.makeText(context.getApplicationContext(), msgStr, Toast.LENGTH_SHORT).show();
            Log.d("RECEIVE", msgStr);
        }
}

1 个答案:

答案 0 :(得分:0)

尝试启用相同的..

<receiver
    android:name="com.smsforwarder.smsforwarder.SMSReceiver"
    **android:enabled="true">**
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>