android - NFC广播问题

标签 android broadcast nfc

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

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

还有我的 list 文件:

<?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);

当应用重新接收它们时,我的接收器会捕获 Intent ,所以我认为接收器没问题,但也许我遗漏了 list 中的某些内容?是否有捕捉全局广播的特殊许可?或者我应该开始服务还是什么?

最佳答案

您无法使用 BroadcastReceiver 捕获这些 Intent ,因为只有 Activity 可以接收 NFC Intent 。您可以在 NFC guide 中找到更多相关信息。 .

关于android - NFC广播问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6829655/

相关文章:

linux - Linux 上的快速用户空间进程间通知方法?

c# - 广播 ping 是如何工作的?

nfc - 由于 RSA key 对生成,安装 java 卡小程序失败

Android IsoDep 命令链失败

android - "IsoDep.get(tag)"调试时返回null

android - 如何在具有多个 ListView 的android中构建复杂布局

android - 如何在 TextView 文本下划线并更改下划线的颜色

android - 如何强制 Marshmallow 打瞌睡?

java - 记录集没有返回任何值

julia - 如何在 Julia 中广播向量内的矩阵?