2012-01-13 18 views

回答

0

首先,你必须得到AndroidMenifest.xml文件的权限。权限是:

<uses-permission android:name="android.permission.NFC" /> 

    <uses-feature android:name="android.hardware.nfc" /> 

将执行NFC读/写操作的行为,在menifest.xml文件活动添加此意图过滤:

 <intent-filter> 
      <action android:name="android.nfc.action.TAG_DISCOVERED" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

在您的活动onCreate()方法你必须初始化NFC适配器和定义待定意图:

NfcAdapter mAdapter; 
    PendingIntent mPendingIntent; 
    mAdapter = NfcAdapter.getDefaultAdapter(this); 
    if (mAdapter == null) { 
     //nfc not support your device. 
     return; 
    } 
    mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 
      getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

在的onResume()回拨使前景调度来检测NFC意图。

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); 

在的onPause()回调您必须禁用于地面调度:

if (mAdapter != null) { 
    mAdapter.disableForegroundDispatch(this); 
} 

在onNewIntent()回调方法,你会得到新的NFC意向。得到意向后,你必须解析检测卡的意图:

@Override 
protected void onNewIntent(Intent intent){  
    getTagInfo(intent) 
    } 
private void getTagInfo(Intent intent) { 
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
} 

现在你有标签。然后,您可以检查标签技术列表以检测该标签。 标签检测技术在这里in My Another Answer

相关问题