Android使用广播接收器发送价值 [英] Android use Broadcast Receivers to send value

查看:92
本文介绍了Android使用广播接收器发送价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个AccessibilityService类:

I have this AccessibilityService class:

public class USSDService extends AccessibilityService {
public static String TAG = "USSDService";
public String responsee="";
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    Log.d(TAG, "onAccessibilityEvent");
    String text = event.getText().toString();

    if (event.getClassName().equals("android.app.AlertDialog")) {
        performGlobalAction(GLOBAL_ACTION_BACK);
        Log.d(TAG, text);
        Intent intent = new Intent("message");
        intent.putExtra("value", text);
        Toast.makeText  (this,text,Toast.LENGTH_LONG).show();

        this.sendBroadcast(intent);// write a broad cast receiver and call sendbroadcast() from here, if you want to parse the message for balance, date
    }

}

@Override
public void onInterrupt() {
}

@Override
protected void onServiceConnected() {
    super.onServiceConnected();
    Log.d(TAG, "onServiceConnected");
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.flags = AccessibilityServiceInfo.DEFAULT;
    info.packageNames = new String[]{"com.android.phone"};
    info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
    setServiceInfo(info);
}}

正如您在方法onAccessibilityEvent中看到的那样,我通过使用方法sendBroadcast发送意图.

As you can see in the method onAccessibilityEvent, I send intent by using the method sendBroadcast.

在MainActivity中,我使用BroadcastReceiver接收这样的值:

In MainActivity I use BroadcastReceiver to receive the value like this:

public class MainActivity extends AppCompatActivity {

private BroadcastReceiver bReceiver = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        //put here whaterver you want your activity to do with the intent received
        Log.i("onReceive",intent.getStringExtra("value") );
    }
};

protected void onResume(){
    super.onResume();
    Log.i("onResume", "22222");
    LocalBroadcastManager.getInstance(this).registerReceiver(bReceiver, new IntentFilter("message"));
}

protected void onPause (){
    super.onPause();
    Log.i("onPause", "11111");

    LocalBroadcastManager.getInstance(this).unregisterReceiver(bReceiver);
}
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);}}

当我调用ussd方法onAccessibilityEvent是有效的并且Toast中显示了值时,我的应用程序运行良好,但是方法onReceive无法工作,并且我不知道问题出在哪里.请帮助我.

My App works fine when I call ussd the method onAccessibilityEvent is work and the value show in Toast, but the method onReceive did not work and I don't know where the problem is. Please help me.

推荐答案

已正确启动的AccessibilityService,并且您的Activity将存在于单独的进程中.这是根据定义.假设您已经正确启动了AccessibilityService,那么下面就是您想要的.

An AccessibilityService that has been launched properly and your Activity are going to exist in separate processes. This is by definition. Assuming that you have launched your AccessibilityService properly, the following is what you want.

public class USSDService extends AccessibilityService {

    public void onAccessibilityEvent(AccessibilityEvent event) {

        Intent intent = new Intent(getPackageName() + "ACTION_ID");
        intent.putExtra("extra_id", "value");

        sendBroadcast(intent);
    }
}

然后您将在活动"中注册此广播,如下所示:

You would then register for this broadcast in your Activity like so:

public class MainActivity extends AppCompatActivity {

    protected void onResume(){
        super.onResume();

        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d("Broadcast", intent.getExtras().getString("extra_id"));
            }
        }, new IntentFilter(getPackageName() + "ACTION_ID"));
    }
}

这里的重要部分是A:您的意图过滤器已绑定到您的包名称,并且"ACTION_ID"始终是相同的字符串.最终,最好通过Activity类或服务类中的公共静态最终字符串来实现此效果,或者在其他一些类中抽象出这一层通信.

Important part here is A: your intent filter is tied to your package name, and that "ACTION_ID" is always the same string. Ultimately this is probably best accomplished by a public static final string in either your Activity class or your service class... or in some other class abstracting away this layer of communication.

这篇关于Android使用广播接收器发送价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆