关于 Android 接收短信的问题

无鸯 发布于 2011/08/09 09:56
阅读 3K+
收藏 1

我尝试创建一个 SMS broadcast receiver ,但总是报 "Permission Denial" 错误,我已经在 manifest 文件增加了权限设定了,如下:

<?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());
                }
            }
        }
    }
}

错误日志信息:

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
从日志你可以看出,已经接收到消息,但要传递给应用时报了权限错误,怎么回事啊?

加载中
0
无鸯
无鸯

原来是缺少阅读短信的权限: 

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

前面的配置只是允许获取短信,但必须增加阅读短信的权限。

0
小強同學
把Receiver里面的permission移除就可以拉
返回顶部
顶部