gpt4 book ai didi

android - 在Android中接收短信(APP崩溃)

转载 作者:行者123 更新时间:2023-12-03 16:53:59 27 4
gpt4 key购买 nike

我试图了解如何接收短信并在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

提前致谢。

问候
普里亚布拉塔

最佳答案

您的接收器类MySMSReceiver很可能不在com.example.smstestbed包中。因此,将接收器类移动到子文件夹com\example\smstestbed\中并添加一个...

package com.example.smstestbed;
import android.content.BroadcastReceiver;
...

public class MySMSReceiver extends BroadcastReceiver {
....

包声明到您的接收器类。

关于android - 在Android中接收短信(APP崩溃),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18291339/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com