Method onNewIntent(intent) not executing in NFC
Asked Answered
S

2

14

I am implementing the NFC in android project. I have written the entries which are required in the AndroidManifest.xml and Java code.

When any tag come near by device then my app detect the tag then it open the activity but here I am getting NFC Tag Info but onNewIntent is not executing.

Please guys share your views.

public class NFCActivity extends Activity {

    private NfcAdapter mNFCAdapter;
    private PendingIntent mNfcPendingIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc);

        // Create the OpenSqliteHelper object. It always best to create only
        // once instance of this OpenSqliteHelper
        DatabaseManager.init(this);

        mNFCAdapter = NfcAdapter.getDefaultAdapter(this);

        if (mNFCAdapter == null) {
            // Device does not support NFC
            Toast.makeText(this,
                    getString(R.string.device_does_not_support_nfc),
                    Toast.LENGTH_LONG).show();
        } else {
            if (!mNFCAdapter.isEnabled()) {
                // NFC is disabled
                Toast.makeText(this, getString(R.string.enable_nfc),
                        Toast.LENGTH_LONG).show();
            } else {
                mNfcPendingIntent = PendingIntent.getActivity(NFCActivity.this,
                        0, new Intent(NFCActivity.this, NFCActivity.class)
                                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
            }
        }
        finish();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Tag mTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mNFCAdapter.disableForegroundDispatch(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mNFCAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null,
                null);
    }
}

AndroidManifest.xml

<activity
    android:name="net.livepatrols.thepartnerSA.NFCActivity"
    android:theme="@android:style/Theme.NoDisplay" >
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
    </intent-filter>

    <meta-data
        android:name="android.nfc.action.TECH_DISCOVERED"
        android:resource="@xml/nfc_tech_filter" />

    <intent-filter>
        <action android:name="android.nfc.action.TAG_DISCOVERED" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
Stook answered 5/2, 2014 at 11:20 Comment(6)
try adding in activity tag android:launchMode="singleInstance" or android:launchMode="singleTop" if issue is related to onNewIntent, it'll solveRobillard
It is already singleTop ` .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);` hereStook
short version is - set the single top flag in both the intent and in the manifest. ReferenceRobillard
Not working I tried your views setting the android:launchMode="singleInstance" or android:launchMode="singleTop"Stook
@Robillard 's solution worked for me but don't know why...Judsen
@BurakKarakuş i.sstatic.net/6C8D9.gifRobillard
C
17

Your code works fine when your activity is on foreground, but it won't work when the Activity is started by an Intent with android.nfc.action.XXX_DISCOVERED action. As it is the first intent, onNewIntent() won't be called, but you can use the intent in your onCreate() method. You should call your NFC logic from onCreate() and from onNewIntent(), validating the action before accessing the tag.

public class NFCActivity extends Activity {

    private NfcAdapter mNFCAdapter;
    private PendingIntent mNfcPendingIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        performTagOperations(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        performTagOperations(intent);
    }

    private void performTagOperations(Intent intent){
        String action = intent.getAction();
        if(action.equals(NfcAdapter.ACTION_TAG_DISCOVERED) ||
        action.equals(NfcAdapter.ACTION_TECH_DISCOVERED) ){
            //PERFORM TAG OPERATIONS
        }
    }
    ...
}
Crowd answered 7/2, 2014 at 11:4 Comment(2)
That makes no sense. Why call performTagOperations twice?Liken
@Liken - This approach doesn't call performTagOperations twice. This is covering two different scenarios. 1)When the activity is launched if a tag is detected and 2) when the activity is the foreground activity. Keep in mind that onNewIntent is only called if the activity is visible.Crowd
T
2

In my case the problem was, that I followed the documentation for nfcAdapter.enableForegroundDispatch(...) blindly and I pasted this code into fragment:

pendingIntent = PendingIntent.getActivity(
            this,
            0,
            new Intent(this, getClass())
                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

Since it actually expects activity, just change all to this to getContext() and especially getClass().

pendingIntent = PendingIntent.getActivity(
            getContext(),
            0,
            new Intent(getContext(), getContext().getClass())
                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
Throughcomposed answered 11/4, 2018 at 14:39 Comment(3)
enableForegroundDispatch requires activity as a paramLiken
set getContext() instead of this saved my day!Scleroprotein
getContext() doesn't existBoutique

© 2022 - 2024 — McMap. All rights reserved.