0

我有一个我正在尝试设置的应用程序,以便当我收到包含某个短语的文本(当前设置为“这是一个测试”只​​是为了测试它)时,将触发一个按钮 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
4

2 回答 2

1

您可以将 MessageReceiver 作为 mainactivity 的内部类,然后,您可以在 mainactivity 中调用 button.performClick

public class MainAcitivity extends Activity
{
   private Button button;
   ....

   ....

   public static class MessageReceiver extends BroadcastReceiver 
   {
       public void onReceive(Context context, Intent intent) 
      {
           button.performClick();
      }
   }

 }


<receiver android:name=".MainActivity$MessageReceiver" android:exported="true">
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>

于 2013-01-31T01:24:49.140 回答
0

在你的MessageReceiver课堂上添加

private static final String SMS_INTENT = "SMS_RECEIVED"; //you can name this whatever

并在您的onReceive方法中添加

 if(messages.getMessageBody().contains("This is a test"))
 {
     //Toast.makeText(context, "It worked", Toast.LENGTH_LONG).show();
     Intent intent = new Intent(SMS_INENT);
     LocalBroadcastManager.sendBroadcast(intent);//this sends a local broadcast within your app
 }

然后在你的MainActivity添加:

private static final String SMS_INTENT = "SMS_RECEIVED"; //you can name this whatever

onCreate

LocalBroadcastManager.registerReceiver(mReceiver, new IntentFilter(SMS_INTENT));

然后在你的创建一个Broadcast receiverMain

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
        // Do something 
                PerformClick():
      }
};

最后记得onDestory在你的Main

@Override
protected void onDestroy() 
{
    super.onDestroy();
        LocalBroadcastManager.unregisterReceiver(mReceiver);

}
于 2013-01-31T01:39:07.730 回答