尽管优先考虑,Android Sms广播接收器仍未触发

时间:2015-12-22 02:25:29

标签: android broadcastreceiver android-manifest

我已经阅读了许多类似的问题,这些问题通常会导致对优先级的回答。在我的案例中,我不认为这是真的,因为我的手机上的其他短信阅读器(如自动化应用程序)接收广播就好了。我想发布我目前正在做的事情的过程,并确保我在我的代码中没有做错会导致此失败。感谢您提供的任何提示!

注意:我已尝试使用最高整数优先级,优先级为99,100,0或根本没有设置。他们都不能工作。

AndroidManifest:

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

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.hennessylabs.drivercompanion.ProcessTextMessage" android:exported="true">
        <intent-filter android:priority="999" android:permission="android.permission.BROADCAST_SMS">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
    </receiver>
    </application>


</manifest>

广播接收器:

package com.hennessylabs.appname;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by kyleC on 11/15/2015.
 */
public class ProcessTextMessage extends BroadcastReceiver {
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    final SmsManager sms = SmsManager.getDefault();
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Entered onReceive", Toast.LENGTH_LONG).show();
        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


                    // Show alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
                    toast.show();

                } // end for loop
            } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);

        }
    }
}

预期结果:

预期的结果是,当SMS到达时,屏幕将首先显示它进入OnReceive方法的Toast。然后它将记录和Toast SMS的内容。

实际结果:

预期结果没有发生任何结果。事实上,即使连接到USB并处于调试模式,它似乎根本不会进入该类。也许我的清单设置错了?

3 个答案:

答案 0 :(得分:1)

如果其他一切都是正确的,那么您似乎错过了RECEIVE_SMS权限。

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

答案 1 :(得分:0)

你遗漏了接收者的清单声明中的一些东西。您必须声明exported=true,并且必须获得BROADCAST_SMS权限。见工作示例:

    <receiver android:name=".sms.IncomingSmsReceiver" android:exported="true">
        <intent-filter android:priority="999" android:permission="android.permission.BROADCAST_SMS">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
    </receiver>

答案 2 :(得分:0)

那么您的应用运行的设备版本是什么?

在Android 4.4中开始,只有默认的短信应用可以接收SMS_DELIVER_ACTION广播。

更多信息:http://android-developers.blogspot.sg/2013/10/getting-your-sms-apps-ready-for-kitkat.html

相关问题