我有一个我正在尝试设置的应用程序,以便当我收到包含某个短语的文本(当前设置为“这是一个测试”只是为了测试它)时,将触发一个按钮 onClick 事件。如果我的消息接收者在我的主要活动中,我可以简单地调用 .performClick()。但是,除非消息接收者在单独的类中,否则我无法让它工作。
消息接收器.java
package com.defsoftsol.snow.alert;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class MessageReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.i("MessageBody", messages.getMessageBody());
if(messages.getMessageBody().contains("This is a test"))
{
Toast.makeText(context, "It worked", Toast.LENGTH_LONG).show();
}
}
}
如何在 MainActivity.java 中的按钮上执行 performClick()?我可以以某种方式从该类访问视图吗?
清单条目
<receiver android:name=".MainActivity.MessageReceiver" android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
在 mainactivity.java 中使用接收器接收文本时,Logcat 崩溃
01-30 20:31:57.002: E/AndroidRuntime(32555): FATAL EXCEPTION: main
01-30 20:31:57.002: E/AndroidRuntime(32555): java.lang.RuntimeException: Unable to instantiate receiver com.defsoftsol.snow.alert.MainActivity.MessageReceiver: java.lang.ClassNotFoundException: com.defsoftsol.snow.alert.MainActivity.MessageReceiver
01-30 20:31:57.002: E/AndroidRuntime(32555): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2100)
01-30 20:31:57.002: E/AndroidRuntime(32555): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
01-30 20:31:57.002: E/AndroidRuntime(32555): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
01-30 20:31:57.002: E/AndroidRuntime(32555): at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 20:31:57.002: E/AndroidRuntime(32555): at android.os.Looper.loop(Looper.java:137)
01-30 20:31:57.002: E/AndroidRuntime(32555): at android.app.ActivityThread.main(ActivityThread.java:4428)
01-30 20:31:57.002: E/AndroidRuntime(32555): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 20:31:57.002: E/AndroidRuntime(32555): at java.lang.reflect.Method.invoke(Method.java:511)
01-30 20:31:57.002: E/AndroidRuntime(32555): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
01-30 20:31:57.002: E/AndroidRuntime(32555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
01-30 20:31:57.002: E/AndroidRuntime(32555): at dalvik.system.NativeStart.main(Native Method)
01-30 20:31:57.002: E/AndroidRuntime(32555): Caused by: java.lang.ClassNotFoundException: com.defsoftsol.snow.alert.MainActivity.MessageReceiver
01-30 20:31:57.002: E/AndroidRuntime(32555): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
01-30 20:31:57.002: E/AndroidRuntime(32555): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-30 20:31:57.002: E/AndroidRuntime(32555): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-30 20:31:57.002: E/AndroidRuntime(32555): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2095)
01-30 20:31:57.002: E/AndroidRuntime(32555): ... 10 more