我正在尝试编写一个广播接收器,该接收器会为每条传入的 SMS 文本消息调用。所有已发布的执行此操作的代码(我能找到)要么已被弃用,要么不起作用。
我的代码在安装时失败,日志中有此消息(两次):
06-17 10:15:59.316 396 413 W ActivityManager: No content provider found for permission revoke: file:///data/local/tmp/locator.apk
06-17 10:15:59.316 396 413 W ActivityManager: No content provider found for permission revoke: file:///data/local/tmp/locator.apk
我的清单文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<receiver android:name=".SmsReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
如果有人能指出我做错了什么,我会很高兴。我开始怀疑没有用于读取传入 SMS 的 API。
我的源代码如下所示:
package com.example;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "locator";
@SuppressWarnings("deprecation")
@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());
}
}
}
}
}