【发布时间】:2014-01-16 15:48:36
【问题描述】:
我正在开发一个 NFC 应用程序。 为了启动我的应用程序,我使用了一个带有 AAR NDEF 记录的 NDEF 标记。
这很好用。但现在我想直接用应用程序读取标签内容。 我怎样才能做到这一点?
(已经可以了,当我从手机上取下标签并再次触摸它时,但我想省略这一步。)
更新:更多细节 好的,为了更清楚,这是我当前代码的一些部分。
private NfcAdapter nfcAdapter;
private static final int PENDING_INTENT_TECH_DISCOVERED = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l);
text_output = (TextView) findViewById(R.id.textView2);
NfcAdapter adapter = ((NfcManager) getSystemService(Context.NFC_SERVICE))
.getDefaultAdapter();
}
@Override
public void onResume() {
super.onResume();
nfcAdapter = ((NfcManager) this.getSystemService(Context.NFC_SERVICE))
.getDefaultAdapter();
PendingIntent pi = createPendingResult(PENDING_INTENT_TECH_DISCOVERED,
new Intent(), 0);
nfcAdapter.enableForegroundDispatch(this, pi,
new IntentFilter[] { new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED) }, new String[][] {
new String[] { "android.nfc.tech.NdefFormatable" },
new String[] { "android.nfc.tech.Ndef" } });
}
@Override
public void onPause() {
super.onPause();
nfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
switch (requestCode) {
case PENDING_INTENT_TECH_DISCOVERED:
Runnable runnable = new Runnable() {
@Override
public void run() {
doTagOperation(data);
}
};
new Thread(runnable).start();
break;
}
}
private void doTagOperation(Intent data) {
try {
String action = data.getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Ndef ndefTag = Ndef.get(tag);
}
} catch (Exception ex) {
...
}
}
要启动应用程序,我在标签上使用了 AAR-NDEF-Record,可以通过以下方式创建: NdefRecord ndr = NdefRecord.createApplicationRecord("com.example.something");
所以我想要的是:在应用程序未启动时触摸标签使应用程序启动(已经工作),然后它应该直接读取标签,而无需将手机从标签上移开并再次触摸它。
【问题讨论】:
-
如果您发布一些当前代码会很有帮助,也许您所说的 onCreate() 方法已经有效。谢谢
标签: android android-intent nfc ndef android-applicationrecord