Android短信广播接收器无法正常工作

时间:2015-04-15 18:27:23

标签: android sms android-broadcast

我'我试图建立一个接收传入短信通知的应用程序。我需要帮助,因为在任何设备上都没有检测到传入的短信。我使用的是以下代码:

http://karanbalkar.com/2014/09/display-incoming-sms-messages-in-android/

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class MySMSApp extends BroadcastReceiver {

public static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if (intent.getAction().equals(ACTION)){ 
        Bundle bundle = intent.getExtras();
        if (bundle != null){
            Object[] pdus = (Object[]) bundle.get("pdus");
            SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++){
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }
            for (SmsMessage message : messages){

                String strMessageFrom = message.getDisplayOriginatingAddress();
                String strMessageBody = message.getDisplayMessageBody();

                Toast.makeText(context, "SMS Message received from:" +strMessageFrom, Toast.LENGTH_LONG).show();
                Toast.makeText(context, "SMS Message content" +strMessageBody, Toast.LENGTH_LONG).show();

            }
        }    
    } 
}

清单如下。我已经设置了intent接收器来触发上面的类,但似乎没有这样做。我无法远程登录模拟器来欺骗传入的短信,因此我对发生的事情视而不见。

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.RECEIVE_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" >
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <receiver android:name=".SMSBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" >
                </action>
            </intent-filter>
        </receiver>
    </activity>
</application>

1 个答案:

答案 0 :(得分:2)

在AndroidManifest.xml中,您使用SMSBroadcastReceiver,但您的BroadcastReceiver班级名称为MySMSApp。您的<receiver>元素也位于<activity>元素内。

尝试使用它:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".MySMSApp">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>