我是 android 新手,我正在使用 android 2.1 来处理一些 sms_receive 的事情:收到短信时,它不会工作......我有一个强制关闭,帮助!
public class SmsReceiverActivity extends Activity {
private static final String SHORTCODE = "55443";
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] messages = (Object[]) bundle.get("pdus");
SmsMessage[] sms = new SmsMessage[messages.length];
//Create messages for each incoming PDU
for (int n = 0; n < messages.length; n++) {
sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
for (SmsMessage msg: sms) {
//Verify if the message came from our known sender
if (TextUtils.equals(msg.getOriginatingAddress(), "SHORTCODE")) {
Toast.makeText(context, "Received message from the mothership: " + msg.getMessageBody(),Toast.LENGTH_SHORT).show();
}
}
}
安卓清单:
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application android:label="@string/app_name">
<activity
android:name=".SmsReceiver"
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" android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
安装程序并且我想运行它时,我强制关闭,这是我的 LogCat:
08-26 12:36:58.531: D/AndroidRuntime(700): Shutting down VM
08-26 12:36:58.531: W/dalvikvm(700): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
08-26 12:36:58.573: E/AndroidRuntime(700): Uncaught handler: thread main exiting due to uncaught exception
08-26 12:36:58.621: E/AndroidRuntime(700): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.smsreceiver/com.example.smsreceiver.SmsReceiver}: java.lang.ClassNotFoundException: com.example.smsreceiver.SmsReceiver in loader dalvik.system.PathClassLoader@44c07980
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.os.Looper.loop(Looper.java:123)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-26 12:36:58.621: E/AndroidRuntime(700): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 12:36:58.621: E/AndroidRuntime(700): at java.lang.reflect.Method.invoke(Method.java:521)
08-26 12:36:58.621: E/AndroidRuntime(700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-26 12:36:58.621: E/AndroidRuntime(700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-26 12:36:58.621: E/AndroidRuntime(700): at dalvik.system.NativeStart.main(Native Method)
08-26 12:36:58.621: E/AndroidRuntime(700): Caused by: java.lang.ClassNotFoundException: com.example.smsreceiver.SmsReceiver in loader dalvik.system.PathClassLoader@44c07980
08-26 12:36:58.621: E/AndroidRuntime(700): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
08-26 12:36:58.621: E/AndroidRuntime(700): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
08-26 12:36:58.621: E/AndroidRuntime(700): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
08-26 12:36:58.621: E/AndroidRuntime(700): ... 11 more
08-26 12:36:58.651: I/dalvikvm(700): threadid=7: reacting to signal 3
08-26 12:36:58.651: E/dalvikvm(700): Unable to open stack trace file '/data/anr/traces.txt': Permission denied