Попытка игнорировать все намерения NFC на переднем плане с помощью enableForegroundDispatch

Я пытаюсь заставить мое приложение игнорировать команды nfc во время работы - оно запускается с помощью тега NFC с записью приложения Android (AAR), и я не хочу, чтобы оно могло запускаться этим, когда оно уже запущено. Я пытался внимательно следовать другим примерам, но приложение все еще может запускаться AAR во время работы (на переднем плане).

manifest.xml:

<application>
    <activity>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/com.MyApp.frontcam" />
        </intent-filter>
    </activity>
</application>

Activity.java:

private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;

@Override
public void onCreate(Bundle savedInstanceState) {

    mAdapter = NfcAdapter.getDefaultAdapter(this);
    mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("*/*");  
    }
    catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    mFilters = new IntentFilter[] {ndef, };
    mTechLists = new String[][] {
            new String[] { NfcA.class.getName() },
            new String[] { Ndef.class.getName() },
            new String[] { NdefFormatable.class.getName() }
    }; 

}

@Override 
protected void onResume() {
    super.onResume();
    mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); //ended up setting mFilters and mTechLists to null
}

2 ответа

Когда вы используете foregroundDispatch новое намерение будет дано вашей активности при сканировании тега. Вы должны перезаписать onNewIntent метод. Добавьте что-то вроде этого:

MainActivity.java

package com.example.nfctest;

import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAdapter = NfcAdapter.getDefaultAdapter(this);
    mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    doSomethingWithIntent(this.getIntent());        
}   

@Override
public void onResume() {
    super.onResume();
    //Enable forground dispatching to get the tags
    mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); //ended up setting mFilters and mTechLists to null
}   

@Override
public void onPause() {
    super.onPause();
    //You need to disable forgroundDispatching here
    mAdapter.disableForegroundDispatch(this);
}   

@Override
public void onNewIntent(Intent data) {
    //Catch the intent your foreground dispatch has launched
    doSomethingWithIntent(data);    
}

private void doSomethingWithIntent(Intent data) 
{ 
    //Get the tag from the given intent
    Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    if(tag != null)
    {
        //Tag is found
        Toast.makeText(this, "Enjoy your tag.", 3).show();      
    }
    else
    {
        //This was an intent without a tag
        Toast.makeText(this, "This was an intent without a tag.", 3).show();    
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nfctest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
 <uses-permission android:name="android.permission.NFC" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Вам не нужно определять SINGLE_TOP режим в вашем манифесте, потому что вы уже сделали это в своем mPendingIntent, И я верю, даже когда вы оставите здесь флаг, он все равно вызовет onNewIntent вместо onCreate,

Вы можете добиться этого, установив launchmode:

<application>
    <activity android:launchmode="singleTask">
    ...

singleTask: система создает действие в корне новой задачи и направляет намерение к ней. Однако, если экземпляр действия уже существует, система направляет намерение к существующему экземпляру посредством вызова его метода onNewIntent() вместо создания нового.

Другие вопросы по тегам