我使用简单的BroadcastReceiver和onReceive方法拦截文本消息。出于某种原因,每次我发送短信时:应用程序崩溃。当我尝试调试应用程序时:我发送文本消息并且应用程序仍然崩溃,但不是将我带入Receiver类,而是暂停在“ActivityThreadhandleReceiver(ActivityThread,ActivityThreadReceiverData)”内部。 Debugging窗口只是为Thread说:“(Suspended(异常RuntimeException))”。
如果我的应用程序不允许我单步执行代码,我该怎么知道我的应用程序崩溃的原因。
这是我的Receiver Class:
public class FinderReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
SmsMessage[ ] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the received message here ---
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();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
// ........first show sms here.....
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PhoneFinder_Activity"
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=".SmsReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
非常感谢任何帮助。谢谢!
编辑:即使重新格式化方法以在我的主Activity中运行,我也得到完全相同的结果。这个完全相同的方法适用于发送文本Intent:
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
String RECEIVED = "SMS_RECEIVED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
PendingIntent receivedPI = PendingIntent.getBroadcast(this, 0, new Intent(RECEIVED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "FRIGGEN SMS!",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(RECEIVED));
答案 0 :(得分:0)
清单文件中有错误 - 接收方的名称必须是类的名称。因此,对于上面的FinderReciever类,您需要:
<receiver android:name=".FinderReciever">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
您还可以通过查看logcat来验证这是否是问题。你应该看到类似的东西:
ERROR/AndroidRuntime(1708): FATAL EXCEPTION: main
ERROR/AndroidRuntime(1708): java.lang.RuntimeException: Unable to instantiate receiver xxx.xxxx.SmsReceiver: java.lang.ClassNotFoundException: ....