2
votes

I am writing a camera app, when the phone read a NFC tag, it will takes a photo

I use this example

https://github.com/josnidhin/Android-Camera-Example

And then modify it

1 Add two properties in CamTestActivity

PendingIntent pendingIntent;
Tag tag;

2 add this in onCreate

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

3 add this in onPause

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);

4 onResume

Log.v("new intent","resume");
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);

5 And then add a new method

@Override
protected void onNewIntent(Intent intent) {

    Log.v("new intent","new intent");
    //preview.performClick();
}

But it does not work

When it read a NFC tag, it will call pause, new intent, resume. It close the activity and start again, but this time it runs onNewIntent rather than new onCreate

I tried many flag, but no one can keep the activity on foreground

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

I found an App on play store, it is called "NFC Camera", it can read NFC tag without restart activity, how to do that?

1
try this you can intent same activity again mNfcPendingIntent = PendingIntent.getActivity(CamTestActivits.this, 0, new Intent(CamTestActivity.this, CamTestActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);M D
Use this, it will call : onPause > onCreate > onResume, so it still restart the activity, and this time it does not call onNewIntent, I cannot get Tag object by tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);CL So

1 Answers

4
votes

In Nfc When new Intent is fired it will go to onPause() method Then it go to onNewIntent() menthod. When It go to onNewIntent() you will get The tag. When You get The tag the you have to call new Intent() to capture camera image using this:

private static final int CAMERA_REQUEST = 1888;
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST);

In that activity , here data will come:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
} 

First of all You have to get permission in AndroidMenifest.xml File for nfc. The permissions are:

 <uses-permission android:name="android.permission.NFC" />

 <uses-feature android:name="android.hardware.nfc" />

The Activity which will perform Nfc Read/write operation , add this intent filter in that activity in menifest.xml file:

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

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

In your activity onCreate() method you have to initialize the NFC adapter and define Pending Intent :

 NfcAdapter mAdapter;
PendingIntent mPendingIntent;
mAdapter = NfcAdapter.getDefaultAdapter(this);   
if (mAdapter == null) {
    //nfc not support your device.
    return;
}
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
        getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

In onResume() Call back enable the Foreground Dispatch to detect NFC intent.

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);

In onPause() callback you must have to disable the forground dispatch:

    if (mAdapter != null) {
    mAdapter.disableForegroundDispatch(this);
}

In onNewIntent() call back method you will get the new Nfc Intent. After getting The Intent , you have to parse the intent to detect the card:

 @Override
protected void onNewIntent(Intent intent){    
    getTagInfo(intent)
     }
private void getTagInfo(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
 //Start for Camera 
}