2
votes

I'm writing an application that makes use od NFC tags. I want NFC tag to be an entry point to my application - closing card containing tag up to the telephone should start my activity. Then, when activity is running, I want to disable all intent filters that can make use of NFC tag, so the another close up would do nothing (I don't want my activity to start all over again). I know how to "disable" my intent filter, using activity aliases:

    <activity-alias android:enabled="true"
            android:name=".alias.NFCEntryPoint"
            android:targetActivity="pl.mitrue.safedb.NFCEntryPoint"
            android:exported="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/alias.pl.mitrue.safedb.NFCEntryPoint" >
        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity-alias>

Then I can programatically disable alias, so my intent filter is disabled too:

getPackageManager().setComponentEnabledSetting(new ComponentName("pl.mitrue.safedb", "pl.mitrue.safedb.alias.NFCEntryPoint"),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

Now here's the problem: I have other applications that use that kind of filter installed on my device. Now, when my application is running and I disabled my intent filter (by disabling activity alias), a dialog appears asking me to choose an application to use. Is there any way to avoid this? In the simplest scenario, once my application is started I don't want to take up any action when closing up NFC tag. Is there any way to make my application the only one that can receive external intents?

For the record: I don't want to turn off NFC completely (I don't know if it is even possible), since I may want to make use of it in other activity later.

I hope I've made my point clear. Please be understanding since I am new here.

Thanks!

1
No, you can't. You are trying to override system security, since you are running sandboxed you can't control this behaviour.Raymond P
Ok, but somehow one of the applications I have installed has desired behaviour. In some places of the application closing up tag do nothing, in other it performs an action (not always the same). I am never asked to choose an activity like I am while using my own application. This is exactly behaviour I need. Maybe there is some kind of workaround I could use? I don't know if it is possible to get and decompile sources of an application installed from Google Market - if so, I will do that if I don't get an answer here.mitrue

1 Answers

1
votes

I was having a similar problem, where: 1) If I was in the cell's start screen (all apps closed), when passing a mifare classic card, open my app automatically, without asking which app want to open if there are many that also reads mifare classic.

2) If my app is open, and pass a card, don't close my app and ask which app use if I have many apps that also reads mifare classic. Solution: use Foreground Dispatch

In my Main Activity:

private IntentFilter[] intentFiltersArray;
private String[][] techListsArray;
private PendingIntent pendingIntent;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pendingIntent = 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("text/plain");
        }
        catch (MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        intentFiltersArray = new IntentFilter[] {ndef, };
        techListsArray = new String[][] { new String[] { android.nfc.tech.MifareClassic.class.getName() } };
    }

@Override
    protected void onResume() {
    super.onResume();
    adaptadorNFC = NfcAdapter.getDefaultAdapter(this);
    System.out.println("Se setearan parametros");
    final Intent intent = new Intent(this.getApplicationContext(), this.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    adaptadorNFC.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);

    leerTarjeta(this.getIntent());
}

@Override
protected void onPause() {
    System.out.println("Se dessetearan parametros");
    adaptadorNFC.disableForegroundDispatch(this);
    super.onPause();
}

Hope this helps other people since this question was asked one year ago :P