【问题标题】:SMS Broadcast Receiver onReceive() method not being called未调用 SMS 广播接收器 onReceive() 方法
【发布时间】:2017-05-31 18:16:49
【问题描述】:

我正在构建一个 SMS 应用程序,其中有一个 one activity 和一个 broadcast receiver。问题是 broadcast receiver onReceive() 方法没有被调用。下面给出了receiver 类和manifest 文件的code。

广播接收器

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);

        }
    }
}

这里的问题是onReceive()下方的Toast消息没有显示,这表明方法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 来测试我的应用程序。 谢谢你的帮助:)

【问题讨论】:

  • 尝试在接收者意图过滤器中设置优先级 android:priority="2147483647"
  • @DanielNugent,我试过了,但没用 :(
  • 您在哪个活动/片段中调用了此接收器?
  • @IbrahimAli,pastebin.com/vXxKcsUu,这是我的MainActivity的链接

标签: android xml android-broadcastreceiver


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多