0
我想捕获传入的短信为我的应用程序。为了做到这一点,我正在使用BroadcastReceiver。我在清单文件中设置了权限和过滤器。每当我收到文本时,应用程序崩溃,当我尝试调试程序时,它甚至不会接收到接收器中的第一行代码。有谁知道我做错了什么?Android短信接收器崩溃
这里是接收器的编码:
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";
}
}
}
}
日志说什么? –
你是指控制台日志,还是调试器中的其他东西?因为在调试器中没有任何可见的东西说没有找到源以外的其他东西。 – user879378
Logcat,我的意思是。您必须首先启用logcat视图。 –