【发布时间】:2014-07-17 09:26:20
【问题描述】:
我正在处理配备 Android NFC 的设备,只是试图读取标签并从 Activity 接收信息。标签的类型为MifareUltralight。只是在Android docs 中阅读了该案例,似乎我首先必须在清单文件中注册 Intent 以启动我的 Activity,然后我才能从中接收 NFC Intent。这就是我的清单文件的样子:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.android">
<uses-feature android:name="android.hardware.nfc" android:required="false" />
<uses-permission android:name="android.permission.NFC" />
<application
android:allowBackup="true"
android:label="Test">
<activity
android:name="com.mycompany.android.activities.NfcTestActivity"
android:launchMode="singleTop"
android:label="Test">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我将 nfc 功能设置为不需要,因为这对我的应用程序运行来说不是必需的。关于 NFC 技术过滤器,这是我的 nfc_tech_filter.xml 文件:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.NfcB</tech>
<tech>android.nfc.tech.NfcF</tech>
<tech>android.nfc.tech.NfcV</tech>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
好吧,配置完成后,我的NfcTestActivity 似乎应该能够接收 NFC Intent。我就是这样实现的:
public class NfcTestActivity extends Activity {
final String TAG = NfcTestActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "IntentAction (onCreate): " + getIntent().getAction());
NfcManager manager = (NfcManager) getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
Log.i(TAG, "NFC adapter is enabled");
} else {
Log.i(TAG, "NFC adapter is disabled");
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.i(TAG, "IntentAction (onNewIntent): " + getIntent().getAction());
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "IntentAction (onResume): " + getIntent().getAction());
}
}
当我启动应用程序时,我会得到以下日志:
05-28 10:41:52.044 31182-31182/com.mycompany.android I/NfcTestActivity? IntentAction (onCreate): android.intent.action.MAIN
05-28 10:41:52.054 31182-31182/com.mycompany.android I/NfcTestActivity? NFC adapter is enabled
05-28 10:41:52.054 31182-31182/com.mycompany.android I/NfcTestActivity? IntentAction (onResume): android.intent.action.MAIN
后来,我阅读了我的标签并显示了此日志:
05-28 10:41:59.661 31182-31182/com.mycompany.android I/NfcTestActivity? IntentAction (onResume): android.intent.action.MAIN
这意味着读取标签时会调用Activity#onResume 方法,正如文档所述,但是,我在这里得到的是android.intent.action.MAIN 而不是NFC 方法(ACTION_NDEF_DISCOVERED、ACTION_TECH_DISCOVERED 或@987654327 @)。
此外,作为线索,当我从我的 Activity 声明中删除这段代码时,当我阅读标签时不会出现 onResume 日志:
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>
看来TAG_DISCOVERED 动作肯定是以某种方式启动的,而不是其他方式,但我无法从活动中正确捕获它。
有人知道吗?
【问题讨论】:
标签: android android-intent nfc intentfilter