响应传入的SMS消息

时间:2011-08-08 22:26:54

标签: android android-intent

我正在尝试创建一个SMS广播接收器,它将在文本消息进入时执行。我已经看过很多关于此的教程但是我收到了一个奇怪的错误“Permission Denial”的效果。

Google搜索此特定错误似乎表明我的清单中没有正确的uses-permission标记,但我确实如此!这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.icemanind.simpletext" android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainWindow" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <receiver android:name=".SMSBroadcastReceiver" android:enabled="true" android:permission="android.permission.RECEIVE_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

以下是该类的java代码:

public class SMSBroadcastReceiver extends BroadcastReceiver {
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private static final String TAG = "SMSBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "Intent recieved: " + intent.getAction());

        if (intent.getAction() == SMS_RECEIVED) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                final SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }
                if (messages.length > -1) {
                    Log.i(TAG,
                            "Message recieved: " + messages[0].getMessageBody());
                }
            }
        }
    }
}

这是我收到的LogCat输出:

D/dalvikvm(  299): GC_EXPLICIT freed 156 objects / 11488 bytes in 144ms
D/dalvikvm(  308): GC_EXPLICIT freed 46 objects / 2176 bytes in 141ms
D/dalvikvm(  169): GC_EXPLICIT freed 642 objects / 36104 bytes in 153ms
W/ActivityManager(   64): Permission Denial: broadcasting Intent { act=android.provider.Telephony.SMS_RECEIVED (hasextras) } from com.android.phone (pid=132, uid=1001) requires android.permission.RECEIVE_SMS due to receiver com.icemanind.simpletext/com.icemanind.simpletext.SMSBroadcastReceiver
V/Telephony(  250): getOrCreateThreadId uri: content://mms-sms/threadID?recipient=6025099968
V/Telephony(  250): getOrCreateThreadId cursor cnt: 1
D/Mms:app (  250): getSmsNewMessageNotificationInfo: count=6, first addr=6025099968, thread_id=1

正如您所看到的,它正在接收文本并尝试将其传递到我的广播接收器上,但是获得了“Permission Denial”错误。任何帮助,将不胜感激。这让我发疯了

2 个答案:

答案 0 :(得分:3)

您需要<uses-permission android:name="android.permission.READ_SMS"/>权限行。目前,您可以获得短信,但不允许实际阅读。将该行添加到您的清单中它就可以了。

答案 1 :(得分:2)

好的,对于其他有此问题的人,我发现了我的问题。我不得不改变这一行:

if (intent.getAction() == SMS_RECEIVED) {

为:

if (intent.getAction().equals(SMS_RECEIVED)) {

显然是我的Java平等问题。