我真的读了10或20个关于这方面的话题,不幸的是我没有让它起作用。我的接收器可以捕获广播,但只有当我从我的应用程序通过sendBroadcast(intent)发送它时。我想让它捕获来自NFC适配器的广播。有人把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);
}
} 和我的清单文件:
<?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);
当应用程序读取它们时,我的接收器捕获意图,所以我认为接收器是好的,但也许我遗漏了清单中的某些东西?是否有获取全球直播的特殊权限?或者我应该开始服务或别的什么?
发布于 2012-11-14 12:32:55
您不能使用BroadcastReceiver捕获这些意图,因为只有活动才能接收NFC意图。您可以在NFC guide中找到有关它的更多信息。
发布于 2013-03-03 00:35:56
我使用in this answer描述的技术-它提供了与广播接收器相同的效果。
发布于 2014-12-05 03:26:07
根据here (https://stackoverflow.com/a/5320694/3736955),你不能通过BroadcastReceiver捕获NFC意图。处理它的唯一方法是通过activity中的ForegroundDispatch和onNewIntent()函数。当NFC标签被点击时,它会寻找前台活动来处理他。
https://stackoverflow.com/questions/6829655
复制相似问题