Android短信接收器崩溃
我正在尝试为我的应用程序捕获传入的sms消息。为了做到这一点,我正在使用广播接收器。我在清单文件中有安装权限和筛选器。每当我收到文本时,应用程序就会崩溃,当我试图调试程序时,它甚至无法到达接收器中的第一行代码。有人知道我做错了什么吗 以下是接收器的代码:Android短信接收器崩溃,android,sms,broadcastreceiver,Android,Sms,Broadcastreceiver,我正在尝试为我的应用程序捕获传入的sms消息。为了做到这一点,我正在使用广播接收器。我在清单文件中有安装权限和筛选器。每当我收到文本时,应用程序就会崩溃,当我试图调试程序时,它甚至无法到达接收器中的第一行代码。有人知道我做错了什么吗 以下是接收器的代码: import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.te
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.os.Bundle;
public class SMSReceiver extends BroadcastReceiver {
String from = null;
String msg = null;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
this.abortBroadcast();
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
from = msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
msg = msgs[i].getMessageBody().toString();
str += "\n";
}
}
}
}
从项目中删除Bin&Gen文件,并在mainfest中注册此接收器
<receiver android:name="yourpackagename.Boot" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_CB_RECEIVED" >
</action>
<action android:name="android.provider.Telephony.SMS_RECEIVED" >
</action>
</intent-filter>
</receiver>
我希望它对您有用,因为它对我有用。您是指控制台日志还是调试器中的其他内容?因为调试器中没有任何可见的东西表明除了找不到源代码之外的任何东西。我的意思是Logcat。您必须首先启用logcat视图。我一直在尝试另一种方法,但我一直遇到的错误是程序无法实例化BroadcastReceiver,它声称错误为找不到类,这意味着该类不在apk文件中。发生了什么事???????我在两个不同的项目中尝试过这个方法,以不同的示例作为起点,示例中的所有内容都有效,但是当我尝试使用广播接收器时,所有内容都崩溃了!!!!