我正在嘗試創建一個短信廣播接收器,每當文本消息進入時都會執行。我已經看到很多關於此的教程,但我正在接收一個奇怪的錯誤「Permission Denial 」。響應傳入的短信
使用谷歌搜索這個特定的錯誤似乎表明我沒有正確的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」錯誤。任何幫助,將不勝感激。這讓我瘋狂
我添加了該權限,但我仍然有同樣的問題,相同的錯誤消息。 – Icemanind
我幾乎相同的'BroadcastReceiver'具有讀取,接收和發送權限,並且工作得很好。 –
你可以發佈你的代碼嗎? – Icemanind