【问题标题】:NFC broadcast problemNFC广播问题
【发布时间】:2011-07-26 11:58:21
【问题描述】:

我确实阅读了 10 或 20 个关于此的主题,但不幸的是我没有让它发挥作用。我的接收器可以捕获广播,但前提是我从我的应用程序通过sendBroadcast(intent) 发送它。我希望它从 NFC 适配器捕获广播。 F.e 有人将 NFC 标签放在我的设备附近,然后我的应用程序应该启动或显示在浏览菜单中,但这不会发生。即使我的应用程序启动,并且我将 NFC Tag 放在设备附近,它也无法捕获它,并且在浏览菜单中我看到其他应用程序可以。 我的接收器:

public class SomeBroadcastReceiver extends BroadcastReceiver {
private final String TAG = "SomeBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Got intent: " + intent);    
}
}    

还有我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nfc">
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name">

<receiver android:enabled="true" android:name=".broadcast.SomeBroadcastReceiver">
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT" />

            <action android:name="android.nfc.action.TECH_DISCOVERED"/>
        <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/technologies"/>

        <action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>        
</receiver>

<activity android:name=".simulator.FakeTagsActivity"
    android:theme="@android:style/Theme.NoTitleBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name="TagViewer"
    android:theme="@android:style/Theme.NoTitleBar">
    <intent-filter>
      <action android:name="android.nfc.action.TAG_DISCOVERED" />
      <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>        
</activity>

</application>
<uses-sdk android:minSdkVersion="10" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />

在 FakeActivity 我有这行:

Intent exampleIntent = new Intent("android.nfc.action.NDEF_DISCOVERED"); sendBroadcast(exampleIntent);

当应用程序重新接收它们时,我的接收器会捕获意图,所以我认为接收器很好,但也许我错过了清单中的某些内容?是否有捕获全球广播的特殊权限?或者我应该开始服务还是什么?

【问题讨论】:

  • 你找到解决这个问题的方法了吗?

标签: android broadcast nfc


【解决方案1】:

您无法使用 BroadcastReceiver 捕获这些意图,因为只有活动才能接收 NFC 意图。您可以在NFC guide 中找到有关它的更多信息。

【讨论】:

    【解决方案2】:

    我使用in this answer 描述的技术 - 它提供与广播接收器相同的效果。

    【讨论】:

      【解决方案3】:

      根据此处 (https://stackoverflow.com/a/5320694/3736955),您无法通过 BroadcastReceiver 捕获 NFC 意图。处理它的唯一方法是通过活动中的 ForegroundDispatch 和 onNewIntent() 函数。当 NFC 标签被点击时,它会寻找前台活动来处理他。

      【讨论】:

        猜你喜欢
        • 2011-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-09
        • 2020-07-21
        • 1970-01-01
        • 2017-06-05
        相关资源
        最近更新 更多