SMS广播接收器onReceive()方法未被调用

时间:2017-05-31 18:16:50

标签: android xml android-broadcastreceiver

我正在构建一个SMS应用程序,有one activitybroadcast receiver。问题是broadcast receiver onReceive()方法没有被调用。 code类和receiver文件的manifest如下所示。

广播接收器

public class SmsBroadcastReceiver extends BroadcastReceiver {
    public AudioManager audioManager;
    SharedPreferences sharedPreferences;
    boolean b=false;
    String smsBody;
    final int REQUEST_CODE_ASK_PERMISSIONS = 123;

    public static final String SMS_BUNDLE = "pdus";

    SmsMessage smsMessage;
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context.getApplicationContext(),"Toast Long",Toast.LENGTH_LONG).show();


        sharedPreferences = context.getApplicationContext().getSharedPreferences("Pritom", context.MODE_PRIVATE);
        Bundle intentExtras = intent.getExtras();
        String format = intentExtras.getString("format");
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
            String pass3 = sharedPreferences.getString("password", null);
            for (int i = 0; i < sms.length; ++i) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                    smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format);
                } else {
                    smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
                }
                smsBody = smsMessage.getMessageBody().toString();
                String address = smsMessage.getOriginatingAddress();

                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";
            }
            /*if (smsBody.equals("@general" + pass3)) {
                AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(audioManager.RINGER_MODE_NORMAL);
            }*/
            Toast.makeText(context, "SMS : " + smsBody + pass3, Toast.LENGTH_SHORT).show();
            Log.d("Message:", smsBody + pass3);

        }
    }
}

此处的问题是,Toast下面的onReceive()消息未显示,表示未调用方法onReceive()

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mytrendin.inappmessaging">
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <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>
        <receiver android:name=".SmsBroadcastReceiver" android:exported="true" >
            <intent-filter android:priority="999" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

我使用Android 5.0.1来测试我的应用程序。 谢谢你的帮助:))

1 个答案:

答案 0 :(得分:0)

尝试在接收方android:priority="2147483647"中设置更高优先级intent-filter

<receiver android:name=".SmsBroadcastReceiver" android:exported="true" >
    <intent-filter android:priority="2147483647" >
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>