传入文本激活按钮

时间:2013-01-31 01:14:11

标签: android button view sms

我有一个应用程序,我正在尝试设置,以便当我收到包含某个短语的文本(当前设置为“这是一个测试”只​​是为了测试它)时,会触发onClick事件按钮。如果我的消息接收器在我的主要活动中,我可以简单地调用.performClick()。但是,除非消息接收者处于单独的类中,否则我无法使其工作。

MessageReceiver.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()?我可以以某种方式从该类访问View吗?

清单条目

<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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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 receiver

中创建Main课程
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);

}