我确实阅读了 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/