gpt4 book ai didi

java - Android 接收短信不起作用

转载 作者:行者123 更新时间:2023-12-01 23:08:09 25 4
gpt4 key购买 nike

我想阅读传入的短信..我创建了这个类来做到这一点:

public class smsRec extends BroadcastReceiver {
private static final String LOG_TAG = "SMSApp";

/* package */ static final String ACTION =
"android.provider.Telephony.SMS_RECEIVED";

// @Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
// if(message starts with SMStretcher recognize BYTE)
StringBuilder sb = new StringBuilder();

/* The SMS-Messages are 'hiding' within the extras of the Intent. */
Bundle bundle = intent.getExtras();
if (bundle != null) {
/* Get all messages contained in the Intent*/
android.telephony.SmsMessage[] messages =
Telephony.Sms.Intents.getMessagesFromIntent(intent);

/* Feed the StringBuilder with all Messages found. */
for (android.telephony.SmsMessage currentMessage : messages){
sb.append("Received compressed SMSnFrom: ");
/* Sender-Number */
sb.append(currentMessage.getDisplayOriginatingAddress());
sb.append("n----Message----n");
/* Actual Message-Content */
sb.append(currentMessage.getDisplayMessageBody());
}
}
/* Logger Debug-Output */
Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);

/* Show the Notification containing the Message. */
Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();

}
}

private void appendData(StringBuilder buf, String key, String value) {
buf.append(", ");
buf.append(key);
buf.append('=');
buf.append(value);
}


}

并在 list 中:

<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

和:<uses-permission android:name="android.permission.RECEIVE_SMS" />

但是我收到一个错误,logcat 是:

03-18 01:00:47.212: E/AndroidRuntime(13808): FATAL EXCEPTION: main
03-18 01:00:47.212: E/AndroidRuntime(13808): Process: com.dd.speech, PID: 13808
03-18 01:00:47.212: E/AndroidRuntime(13808): java.lang.RuntimeException: Unable to instantiate receiver com.dd.speech.SMSReceiver: java.lang.ClassNotFoundException: Didn't find class "com.dd.speech.SMSReceiver" on path: DexPathList[[zip file "/data/app/com.dd.speech-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.dd.speech-2, /vendor/lib, /system/lib]]
03-18 01:00:47.212: E/AndroidRuntime(13808): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2420)
03-18 01:00:47.212: E/AndroidRuntime(13808): at android.app.ActivityThread.access$1700(ActivityThread.java:145)
03-18 01:00:47.212: E/AndroidRuntime(13808): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
03-18 01:00:47.212: E/AndroidRuntime(13808): at android.os.Handler.dispatchMessage(Handler.java:102)
03-18 01:00:47.212: E/AndroidRuntime(13808): at android.os.Looper.loop(Looper.java:136)
03-18 01:00:47.212: E/AndroidRuntime(13808): at android.app.ActivityThread.main(ActivityThread.java:5081)
03-18 01:00:47.212: E/AndroidRuntime(13808): at java.lang.reflect.Method.invoke(Native Method)
03-18 01:00:47.212: E/AndroidRuntime(13808): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-18 01:00:47.212: E/AndroidRuntime(13808): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
03-18 01:00:47.212: E/AndroidRuntime(13808): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.dd.speech.SMSReceiver" on path: DexPathList[[zip file "/data/app/com.dd.speech-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.dd.speech-2, /vendor/lib, /system/lib]]
03-18 01:00:47.212: E/AndroidRuntime(13808): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-18 01:00:47.212: E/AndroidRuntime(13808): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-18 01:00:47.212: E/AndroidRuntime(13808): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
03-18 01:00:47.212: E/AndroidRuntime(13808): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2415)
03-18 01:00:47.212: E/AndroidRuntime(13808): ... 8 more
03-18 01:00:47.212: E/AndroidRuntime(13808): Suppressed: java.lang.ClassNotFoundException: com.dd.speech.SMSReceiver
03-18 01:00:47.212: E/AndroidRuntime(13808): at java.lang.Class.classForName(Native Method)
03-18 01:00:47.212: E/AndroidRuntime(13808): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
03-18 01:00:47.212: E/AndroidRuntime(13808): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
03-18 01:00:47.212: E/AndroidRuntime(13808): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
03-18 01:00:47.212: E/AndroidRuntime(13808): ... 10 more
03-18 01:00:47.212: E/AndroidRuntime(13808): Caused by: java.lang.NoClassDefFoundError: Class "Lcom/dd/speech/SMSReceiver;" not found
03-18 01:00:47.212: E/AndroidRuntime(13808): ... 14 more

我找不到问题

最佳答案

java 中的类名和 list 必须匹配。现在一个叫SMSReceiver,另一个叫smsRec

关于java - Android 接收短信不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22467875/

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