用于 NFC 的程序化意图过滤器

发布于 2024-11-09 18:58:25 字数 1592 浏览 0 评论 0原文

编辑:我找到了解决方案,请参见下文

我在 StackOverFlow 上的第一篇文章。然而,我已经阅读有关这个​​问题一段时间了,但没有有效的解决方案。

我想做的是注册以下意图: android.nfc.action.TAG_DISCOVERED

我在代码中执行以下操作:

    IntentFilter filter = new IntentFilter();
    filter.addAction("android.nfc.action.TAG_DISCOVERED");
    filter.addCategory("android.intent.category.DEFAULT");
    Log.d(TAG, "Created the new filter");
    reciever = new NFCBroadcastReciever(this);
    Log.d(TAG, "Created the new Broadcast Reciever");
    this.registerReceiver(reciever, filter);
    Log.d(TAG, "Registered new reciever");

BroadCastReciever 定义如下:

public class NFCBroadcastReciever extends BroadcastReceiver {

private Screen screen;
public static String TAG = "NFCBroadcastReciever";

NFCBroadcastReciever(Screen _screen){
    screen = _screen;
}

@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    Log.d(TAG, "Action recieved: "+action);
    if(action != null && NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)){
        paymentScreen.onNewIntent(intent);
    }
}

}

但是我得到一个例外,该意图是从标签读取触发没有相应的活动。我希望能够仅在应用程序中的某个时刻开始监听 NFC 事件。

预先感谢您的帮助。

我实际上找到了问题的解决方案,关键是让 NFC 事件仅在特定活动处于活动状态时发生,而不是在其他活动运行时发生。 Android SDK 中的示例对此进行了解释: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/nfc/ForegroundDispatch.html

EDIT: I found the solution, see below

My first post on StackOverFlow. However I have been reading about this problem for a while without a solution that works.

What I would like to do is register the following Intent: android.nfc.action.TAG_DISCOVERED

I am doing the following in my Code:

    IntentFilter filter = new IntentFilter();
    filter.addAction("android.nfc.action.TAG_DISCOVERED");
    filter.addCategory("android.intent.category.DEFAULT");
    Log.d(TAG, "Created the new filter");
    reciever = new NFCBroadcastReciever(this);
    Log.d(TAG, "Created the new Broadcast Reciever");
    this.registerReceiver(reciever, filter);
    Log.d(TAG, "Registered new reciever");

The BroadCastReciever is defined as follows:

public class NFCBroadcastReciever extends BroadcastReceiver {

private Screen screen;
public static String TAG = "NFCBroadcastReciever";

NFCBroadcastReciever(Screen _screen){
    screen = _screen;
}

@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    Log.d(TAG, "Action recieved: "+action);
    if(action != null && NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)){
        paymentScreen.onNewIntent(intent);
    }
}

}

However I get an exception that the intent being fired from a tag read has no corresponding Activity. I would like to be able to only start listening for NFC events at a certain point in my application.

Thanks in advance for your help.

I found the solution to the problem actually, the key to getting NFC events to occur only on a specific activity while it is active and not when other activities are running. The sample in the Android SDK explains it: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/nfc/ForegroundDispatch.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白首有我共你 2024-11-16 18:58:25

我实际上找到了问题的解决方案,关键是让 NFC 事件仅在特定活动处于活动状态时发生,而不是在其他活动运行时发生。 Android SDK 中的示例对此进行了解释: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/nfc/ForegroundDispatch.html

I found the solution to the problem actually, the key to getting NFC events to occur only on a specific activity while it is active and not when other activities are running. The sample in the Android SDK explains it: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/nfc/ForegroundDispatch.html

拍不死你 2024-11-16 18:58:25

您是否打算在收到广播后开始活动?在我看来, paymentScreen.onNewIntent(intent); 不会实现这一点。相反,您可能需要构建一个可与 startActivity() 一起使用的意图,并且您可能希望以附加形式将广播接收器意图中的相关数据包含到您的活动意图中。

Is your intention to start an activity when the broadcast is received? It doesn't seem to me that paymentScreen.onNewIntent(intent); is going to accomplish that. Instead, you will likely need to build an intent that you can use with startActivity() and you'll likely want to include the relevant data from your broadcast receiver's intent into your activity intent in the form of extras.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文