开发者

Complete Action Using (avoid dialog when app is already running)

I'm developing an Android application to catch NFC tag information. The device I use is a Google Nexus S.

When i searched this subject, I found lots of information about getting my application in the "Complete action using" dialog. This is working fine now, when I read a RFID tag, I'm able to select my application and I parse the INTENT.

A couple NFC apps from NXP show op in the "Complete action using" dialog too but when one of this applications is active (foreground) I can read tags WITHOUT GETTING QUESTIONED AGAIN what app to open.

My question: How can I PREVENT the "Complete action using" dialog when my app is already running like NXP does?

Note: My application is already set to single instance mode.

Here is a snippet of my code:

package nfc.test;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.nfc.*;
import android.nfc.tech.*;
import android.os.Bundle;

public class NfcActivity extends Activity {

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        

        mAdapter = NfcAd开发者_StackOverflowapter.getDefaultAdapter(this);
        mPendingIntent = PendingIntent.getActivity(
                this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        IntentFilter techFilter = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        IntentFilter tagFilter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        IntentFilter ndefFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

        try {
            techFilter.addDataType("*/*");      // Handles all MIME based dispatches. 
            tagFilter.addDataType("*/*");       // You should specify only the ones that you need.
            ndefFilter.addDataType("*/*");
        } catch (MalformedMimeTypeException e) {
            DialogHelper.showErrorDialog(this, "MalformedMimeTypeException" + e.getMessage());
        }

        mFilters = new IntentFilter[] {
                techFilter,
                tagFilter,
                ndefFilter
        };

        mTechLists = new String[][] { new String[] { 
                IsoDep.class.getName(),             // ISO 14443-4
                MifareClassic.class.getName(),      // Mifare Classic
                MifareUltralight.class.getName(),   // Mifare Ultra Light
                Ndef.class.getName(),               // NFC Forum Type 1, 2, 3, 4 Compliant Tags         
                NdefFormatable.class.getName(),     // Can be used as NDEF tag
                NfcA.class.getName(),               // ISO 14443-3A
                NfcB.class.getName(),               // ISO 14443-3B
                NfcF.class.getName(),               // JIS 6319-4
                NfcV.class.getName()                // ISO 15693
        } };
    }

    @Override
    public void onPause() {
        super.onPause();
        mAdapter.disableForegroundDispatch(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
    }

    @Override
    public void onNewIntent(Intent intent) {        
        super.setIntent(intent);
        resolveIntent(intent);
    }

    public void resolveIntent(Intent intent) {

        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())
                 || NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())
                 || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {

            Tag tag = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

            // Handle tag
        }
    }
}


How can I PREVENT the "Complete action using" dialog when my app is already running like NXP does?

Use enableForegroundDispatch() to indicate the tag events that you handle. Here is a sample project demonstrating this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜