我有一个应用程序试图从特定发件人捕获SMS消息,然后开始一个新的活动。当使用Debug As在Eclipse中运行并使用我的BroadcastReceiver模拟器时,它运行良好,它按预期命中断点。当我在Eclipse中使用Run As在模拟器中启动时,SMS消息永远不会被捕获。似乎从不基于缺少日志输出调用BroadcastReceiver。在我的手机上运行时,短信也不会被抓住。任何想法为什么它以一种方式而不是另一种方式工作?
我手机上的应用程序非常少,而且我所知道的唯一一个可以捕获短信的应用就是内置的信使。
<receiver android:name=".sms.ConfirmationResponder">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
...
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
接收者:
public class ConfirmationResponder extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("SMS", "############################ Confirmation being read");
// More stuff after this but I don't even get the log message
}
}
答案 0 :(得分:2)
这是我的计划:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
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());
}
}
}
}
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Technicaljar.SMSBroadcastReceiver"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" >
<receiver android:name=".SMSBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
希望它有助于
答案 1 :(得分:0)
我认为它有效,但当“运行方式”模式并且不显示logcat信息时,eclipse不会附加到设备上。