我正在编写一个简单的代码,当我收到一条消息时启动一个广播接收器。但我得到一个强制关闭错误。我在 AndroidManifest.xml 添加了所需的权限。我从这个站点(http://www.androidcompetencycenter.com/2008/12/android-api-sms-handling/)获取了 BroadcastReceiever 的代码。请告诉我哪里出错了。这是我的代码:
package com.example.findme;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);*/
Toast.makeText(context,"Reveived!",1).show();
}
// show first message
Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
}
这是logCat:
11-09 21:05:03.190: D/AndroidRuntime(446): Shutting down VM
11-09 21:05:03.190: W/dalvikvm(446): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-09 21:05:03.230: E/AndroidRuntime(446): FATAL EXCEPTION: main
11-09 21:05:03.230: E/AndroidRuntime(446): java.lang.RuntimeException: Unable to instantiate receiver com.example.smsmanager1.receiver.SMSReceiver: java.lang.ClassNotFoundException: com.example.smsmanager1.receiver.SMSReceiver in loader dalvik.system.PathClassLoader[/data/app/com.example.smsmanager1-1.apk]
11-09 21:05:03.230: E/AndroidRuntime(446): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789)
11-09 21:05:03.230: E/AndroidRuntime(446): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
11-09 21:05:03.230: E/AndroidRuntime(446): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
11-09 21:05:03.230: E/AndroidRuntime(446): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 21:05:03.230: E/AndroidRuntime(446): at android.os.Looper.loop(Looper.java:123)
11-09 21:05:03.230: E/AndroidRuntime(446): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-09 21:05:03.230: E/AndroidRuntime(446): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 21:05:03.230: E/AndroidRuntime(446): at java.lang.reflect.Method.invoke(Method.java:521)
11-09 21:05:03.230: E/AndroidRuntime(446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-09 21:05:03.230: E/AndroidRuntime(446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-09 21:05:03.230: E/AndroidRuntime(446): at dalvik.system.NativeStart.main(Native Method)
11-09 21:05:03.230: E/AndroidRuntime(446): Caused by: java.lang.ClassNotFoundException: com.example.smsmanager1.receiver.SMSReceiver in loader dalvik.system.PathClassLoader[/data/app/com.example.smsmanager1-1.apk]
11-09 21:05:03.230: E/AndroidRuntime(446): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
11-09 21:05:03.230: E/AndroidRuntime(446): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
11-09 21:05:03.230: E/AndroidRuntime(446): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
11-09 21:05:03.230: E/AndroidRuntime(446): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2780)
11-09 21:05:03.230: E/AndroidRuntime(446): ... 10 more
11-09 21:05:13.211: I/Process(446): Sending signal. PID: 446 SIG: 9