上位レベル (NDEF_DISCOVERED または TECH_DISCOVERED) で同じタグを管理できる他のアプリがある場合、下位レベルのみを管理するアプリは呼び出されません。
アプリを使用するには、タグをスキャンするよりも、アプリを開く必要があります。
アプリが起動しない場合は、次の手順を完了していることを確認してください。
OnCreate() で NfcAdapter を取得します。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
finish();
return;
}
//method to handle your intent
handleTag(getIntent());
}
onResume で、フォアグラウンド ディスパッチを有効にします。
@Override
public void onResume() {
super.onResume();
final Intent intent = new Intent(this.getApplicationContext(), this.getClass());
final PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, 0);
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
onPause で無効にします。
@Override
protected void onPause() {
mNfcAdapter.disableForegroundDispatch(this);
super.onPause();
}
onNewIntent で、関数を呼び出してインテントを処理します
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleTag(intent);
}
関数でタグを処理します。
private void handleTag(Intent intent){
String action = intent.getAction();
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action){
// here your code
}
}
マニフェストにアクセス許可を追加することを忘れないでください。
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<activity>
....
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>
</activity>
詳細はこちらNFC BasisおよびこちらNFCタグを読む