从当前应用程序中独家读取NFC标签

时间:2016-06-13 01:08:41

标签: android android-intent nfc android-6.0-marshmallow intentfilter

我试图从我的应用程序中读取NFC标签,但每次我扫描标签时都会打开一个默认应用程序(我认为它是Android的默认应用程序......)

我想阻止任何标记在我的应用程序打开时被读取。 我怎么能做到这一点?

ScanNFC.java(读取NFC数据的活动):

public class ScanNFC extends AppCompatActivity {


public static final String MIME_TEXT_PLAIN = "text/plain";
public static final String TAG = "NfcDemo";

private NfcAdapter mNfcAdapter;

private TextView mTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan_nfc);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null)
    {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    mTextView = (TextView) findViewById(R.id.tvTapNFC);
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);


    if (mNfcAdapter != null) {
        mTextView.setText("Read an NFC tag");
    } else {
        mTextView.setText("This phone is not NFC enabled.");
    }


    handleIntent(getIntent());
}


@Override
protected void onResume() {
    super.onResume();

    setupForegroundDispatch(this, mNfcAdapter);
}

@Override
protected void onPause() {

    stopForegroundDispatch(this, mNfcAdapter);

    super.onPause();
}

@Override
protected void onNewIntent(Intent intent) {

    handleIntent(intent);
}


private void handleIntent(Intent intent)
{
    String action = intent.getAction();


    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

        String type = intent.getType();
        if (MIME_TEXT_PLAIN.equals(type)) {

            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            new AsyncNdefReaderTask().execute(tag);

        } else {
            Log.d(TAG, "Wrong mime type: " + type);
        }
    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        String[] techList = tag.getTechList();
        String searchedTech = Ndef.class.getName();

        for (String tech : techList) {
            if (searchedTech.equals(tech)) {
                new AsyncNdefReaderTask().execute(tag);
                break;
            }
        }
    }
}


private class AsyncNdefReaderTask extends NdefReaderTask
{
    @Override
    protected void onPostExecute(String result) {

        Log.d("onPostExecute", "onPostExecute");

        if (result != null) {
            mTextView.setText("Read content: " + result);
        }
    }
}


public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter)
{
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][]{};

    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        filters[0].addDataType(MIME_TEXT_PLAIN);
    } catch (IntentFilter.MalformedMimeTypeException e) {
        throw new RuntimeException("Check your mime type.");
    }

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}


public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    adapter.disableForegroundDispatch(activity);
}

}

的AndroidManifest.xml:

<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />


<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".ScanNFC"
        android:label="@string/title_activity_scan_nfc"
        android:theme="@style/AppTheme.NoActionBar">

        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain" />
            <data android:scheme="http" />
        </intent-filter>

    </activity>
</application>

如何在我的应用运行时阻止其他应用阅读NFC标签?

1 个答案:

答案 0 :(得分:3)

关键是要使用前台调度系统(NfcAdapter.enableForegroundDispatch())或阅读器模式API(NfcAdapter.enableReaderMode())。请注意,后者仅适用于Android 4.4 +。

您已尝试在代码中使用前台调度系统。但是,您目前只注册一个非常特定的标记类型(可能与您的标记不匹配):

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][]{};

    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        filters[0].addDataType(MIME_TEXT_PLAIN);
    } catch (IntentFilter.MalformedMimeTypeException e) {
        throw new RuntimeException("Check your mime type.");
    }

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}

使用该代码注册只接收包含NDEF文本记录(或文本/纯MIME类型记录)的标记的事件。

您只需使用以下命令注册即可接收任何标记的通知:

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity, activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);

    adapter.enableForegroundDispatch(activity, pendingIntent, null, null);
}