我试图了解如何接收短信并在android中获取其内容。 无论我尝试哪个例子,包括许多来自堆栈交换,每次,我的应用程序在收到短信时崩溃。我无法找到我做错的事。需要帮助。
代码:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;
public class MySMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Object[] pdus=(Object[])intent.getExtras().get("pdus");
SmsMessage shortMessage=SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.d("SMSReceiver","SMS message sender: "+
shortMessage.getOriginatingAddress());
Log.d("SMSReceiver","SMS message text: "+
shortMessage.getDisplayMessageBody());
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smstestbed"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.smstestbed.MainActivity"
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=".MySMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
logcat的:
08-17 17:41:28.762: E/Trace(2647): error opening trace file: No such file or directory (2)
08-17 17:41:29.612: D/libEGL(2647): loaded /system/lib/egl/libEGL_emulation.so
08-17 17:41:29.622: D/(2647): HostConnection::get() New Host Connection established 0x2a174ab8, tid 2647
08-17 17:41:29.622: D/libEGL(2647): loaded /system/lib/egl/libGLESv1_CM_emulation.so
08-17 17:41:29.632: D/libEGL(2647): loaded /system/lib/egl/libGLESv2_emulation.so
08-17 17:41:29.682: W/EGL_emulation(2647): eglSurfaceAttrib not implemented
08-17 17:41:29.692: D/OpenGLRenderer(2647): Enabling debug mode 0
08-17 17:42:06.712: D/AndroidRuntime(2647): Shutting down VM
08-17 17:42:06.712: W/dalvikvm(2647): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-17 17:42:06.762: E/AndroidRuntime(2647): FATAL EXCEPTION: main
08-17 17:42:06.762: E/AndroidRuntime(2647): java.lang.RuntimeException: Unable to instantiate receiver com.example.smstestbed.MySMSReceiver: java.lang.ClassNotFoundException: Didn't find class "com.example.smstestbed.MySMSReceiver" on path: /data/app/com.example.smstestbed-1.apk
08-17 17:42:06.762: E/AndroidRuntime(2647): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2357)
08-17 17:42:06.762: E/AndroidRuntime(2647): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
08-17 17:42:06.762: E/AndroidRuntime(2647): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
08-17 17:42:06.762: E/AndroidRuntime(2647): at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 17:42:06.762: E/AndroidRuntime(2647): at android.os.Looper.loop(Looper.java:137)
08-17 17:42:06.762: E/AndroidRuntime(2647): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-17 17:42:06.762: E/AndroidRuntime(2647): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 17:42:06.762: E/AndroidRuntime(2647): at java.lang.reflect.Method.invoke(Method.java:511)
08-17 17:42:06.762: E/AndroidRuntime(2647): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-17 17:42:06.762: E/AndroidRuntime(2647): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-17 17:42:06.762: E/AndroidRuntime(2647): at dalvik.system.NativeStart.main(Native Method)
08-17 17:42:06.762: E/AndroidRuntime(2647): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.smstestbed.MySMSReceiver" on path: /data/app/com.example.smstestbed-1.apk
08-17 17:42:06.762: E/AndroidRuntime(2647): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
08-17 17:42:06.762: E/AndroidRuntime(2647): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
08-17 17:42:06.762: E/AndroidRuntime(2647): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-17 17:42:06.762: E/AndroidRuntime(2647): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2352)
08-17 17:42:06.762: E/AndroidRuntime(2647): ... 10 more
提前致谢。
此致 Priyabrata
答案 0 :(得分:1)
您的接收者班级MySMSReceiver
很可能不在包com.example.smstestbed
中。因此,将接收器类移动到子文件夹com\example\smstestbed\
并添加...
package com.example.smstestbed;
import android.content.BroadcastReceiver;
...
public class MySMSReceiver extends BroadcastReceiver {
....
将包声明发送给您的接收器类。