阅读NFC标签中包含Android Play应用的内容
问题描述:
我有一个标签,其中包含Android Play应用的内容。当手机靠近它时,它会默认打开Android Play商店的应用程序页面。 现在我想要做的是写一个应用程序可以读取这个标签。所以我修改的AndroidManifest.xml如下:阅读NFC标签中包含Android Play应用的内容
<uses-permission android:name="android.permission.NFC" />
......
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="ext"
android:pathPrefix="/android.com:pkg"
android:scheme="vnd.android.nfc" />
</intent-filter>
......
我用另一个标签应用才能阅读标签,它有内容,如:
NDEF message
EXTERNAL: urn:nfc:ext:android.com:pkg
com.example.app
我认为最关键的一点是意图过滤器。但是当我接近这个标签时,我无法午餐。无论我的应用程序是在后台还是前台。有人可以帮助我吗?谢谢。
答
我刚找到答案。我误解了前景的意义,让这款APP在前台运行。它具有enableForegroundDispatch添加如下:
private NfcAdapter nfcAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
protected void onCreate(Bundle savedInstanceState)
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndefPkg = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndefPkg.addDataScheme("vnd.android.nfc");
ndefPkg.addDataAuthority("ext", null);
ndefPkg.addDataPath("/android.com:pkg", 0);
} catch (Exception e) {
e.printStackTrace();
}
mFilters = new IntentFilter[] {ndefPkg;
mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
}
protected void onResume() {
super.onResume();
nfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
}
protected void onPause() {
super.onPause();
nfcAdapter.disableForegroundDispatch(this);
}
[检查这一点,如果它有助于(http://stackoverflow.com/a/26698189/2591002),如果你有其他的应用程序,它也被注册了同样的行动,你会被给予对话来选择你想要打开的应用程序。 – 2014-11-08 13:56:27
在前台没有应用程序时,没有对话选择应用程序。我想如果几个应用程序注册了相同的Action,前台应用程序将默认为午餐。 – 2014-11-08 14:02:53
但如果没有应用程序在前台,那么它也打开应用程序(如果只有一个)或显示对话..你检查链接? – 2014-11-08 14:04:20