如何从HostApduService发送消息到活动? [英] How can I send message from HostApduService to an activity?

查看:151
本文介绍了如何从HostApduService发送消息到活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想轻松地将字符串传递给Activity.之所以需要回调,是因为当必须传递字符串时,Activity必须执行某些操作.

I would like to pass a string to an Activity easily. Something like callback would be needed because when the string has to be passed then the Activity has to do something.

public class MyHostApduService extends HostApduService {
    @Override
    public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
        if (selectAidApdu(apdu)) {
            Log.i("HCEDEMO", "Application selected");
            return getWelcomeMessage();
        }
        else {
            if (exchangeDataApdu(apdu)) {
                Log.i("HCEDEMO", "Data exchanged");

                // int _len_ = apdu[4];
                String _data_ = new String(apdu).substring(5, apdu.length - 1);
                // TODO: Send _data_ to an activity...

                return new byte[] { (byte)0x90, (byte)0x00 };
            }
            else {
                Log.i("HCEDEMO", "Received: " + new String(apdu));
                return getNextMessage();
            }
        }
    }
}

推荐答案

您的问题非常广泛,答案取决于您未向我们透露的应用程序的某些设计方面:

Your question is very broad and an answer depends on some design aspects of your application that you did not reveal to us:

您可以以一种意图开始活动,并将参数作为额外的意图进行传递(另请参见此答案):

You can start the activity with an intent and pass the parameter as intent extra (also see this answer):

Intent intent = new Intent(this, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("hcedata", _data_)
this.startActivity(intent);

您要将数据传递到活动的正在运行的实例

在这种情况下,例如,您可以从活动中注册BroadcastReceiver:

public class YourActivity extends Activity {
    final BroadcastReceiver hceNotificationsReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if () {
                String hcedata = intent.getStringExtra("hcedata");
                // TODO: do something with the received data
            }
        }
    });

    @Override
    protected void onStart() {
        super.onStart();

        final IntentFilter hceNotificationsFilter = new IntentFilter();
       hceNotificationsFilter.addAction("your.hce.app.action.NOTIFY_HCE_DATA");
        registerReceiver(hceNotificationsReceiver, hceNotificationsFilter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(hceNotificationsReceiver);
    }

然后,在您的服务中,您可以向该广播接收方发送一个意图(请注意,您可能希望在具有接收方许可的情况下限制广播,以防止您的数据泄露给未经授权的接收方):

In your service, you could then send an intent to that broadcast receiver (note that you might want to restrict the broadcast with a receiver permission in order to prevent your data from leaking to unauthorized receivers):

Intent intent = new Intent("your.hce.app.action.NOTIFY_HCE_DATA");
intent.putExtra("hcedata", _data_)
this.sendBroadcast(intent);

您想将数据传递给活动并接收回响应到您的服务中

与上述类似,您可以在服务中使用广播接收器以从活动中获得通知.请注意,您无法绑定到您的HCE服务,因此您不能直接从活动中对其调用方法.

You want to pass data to an activity and receive a response back into your service

Similar as above, you could use the broadcast receiver in your service to get notified from your activity. Note that you cannot bind to your HCE service, so you cannot directly invoke methods on it from your activity.

这篇关于如何从HostApduService发送消息到活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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