Responding to incoming SMS messages [closed]
I am trying to create an SMS broadcast receiver that will execute whenever a text message comes in. I have seen a lot of tutorials on this but I am receiving a curious error to the effect of "Permission Denial".
Googling this particular error seems to indicate that I do not have the proper uses-permission
tag in my manifest, but I do! Here is my manifest file:
<?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>
Here is the java code for the class:
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].getMessageBo开发者_如何学Pythondy());
}
}
}
}
}
Here is the LogCat output I am receiving:
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
As you can see, it is receiving the text and attempting to pass it onto my broad cast receiver, but gets that "Permission Denial" error. Any help would be appreciated. This is driving me crazy
You need the <uses-permission android:name="android.permission.READ_SMS"/>
permission line. Currently, you're allowed to get the SMS, but not allowed to actually read it. Add that line to your manifest and it'll work.
Okay, for anyone else having this issue, I found my problem. I had to change this line:
if (intent.getAction() == SMS_RECEIVED) {
to:
if (intent.getAction().equals(SMS_RECEIVED)) {
A Java equality issue apparently on my end.
精彩评论