Skip to content Skip to sidebar Skip to footer

Trying To Ignore All Nfc Intents While In Foreground With Enableforegrounddispatch

I'm trying to get my app to ignore nfc commands while running - its launched by an NFC tag with an Android Application Record (AAR), and I dont want it to be able to be launched by

Solution 1:

You can achieve this by setting launchmode:

<application><activityandroid:launchmode="singleTask">
    ...

singleTask: The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

Solution 2:

When you use foregroundDispatch a new intent will be given to your activity when you scan a tag. You should overwrite the onNewIntent method. Add something like this:

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;

publicclassMainActivityextendsActionBarActivity {

privateNfcAdapter mAdapter;
privatePendingIntent mPendingIntent;

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

    doSomethingWithIntent(this.getIntent());        
}   

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

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

@OverridepublicvoidonNewIntent(Intent data) {
    //Catch the intent your foreground dispatch has launcheddoSomethingWithIntent(data);    
}

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

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);
    returntrue;
}

@OverridepublicbooleanonOptionsItemSelected(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) {
        returntrue;
    }
    returnsuper.onOptionsItemSelected(item);
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example.nfctest"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="21" /><uses-permissionandroid:name="android.permission.NFC" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".MainActivity"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

You don't need to define the SINGLE_TOP mode in your manifest because you already did this in your mPendingIntent. And i beleave, even when you leave the flagg here to, it still will call the onNewIntent instead of the onCreate.

Post a Comment for "Trying To Ignore All Nfc Intents While In Foreground With Enableforegrounddispatch"