Android : NFC enableForegroundDispatch to handle Beam within activity

on Thursday, July 10, 2014


the app i'm working on ought to receive a beam and then call the processIntent(intent) function from onResume. the problem i'm having is that when it receives the beam, it opens a completely new instance of the app instead of just staying within the activity (i've already called enableForegroundDispatch).



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null){
Toast.makeText(this, "No NFC on this device", Toast.LENGTH_LONG).show();
}
// Create a PendingIntent object so the Android system can populate it with the details of the tag when it is scanned.
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Declare intent filters to handle the intents that the developer wants to intercept.
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
Log.wtf("mimeexception", e);
e.printStackTrace();
}
intentFiltersArray = new IntentFilter[] {ndef};

... ...
}

public void onResume() {
super.onResume();
// Enable the foreground dispatch when the Activity regains focus.
NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this, pendingIntent, intentFiltersArray, null);

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
processIntent(getIntent());
}
}

public void onPause() {
super.onPause();
// Disable the foreground dispatch when the Activity loses focus.
NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
}

void processIntent(Intent intent){
// do stuff
}


and here's the activity from the manifest



<activity
android:name="com.nextixsystems.ewalletv2.CreateInvoiceActivity"
android:label="@string/title_activity_create_invoice"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />\
<category android:name="android.intent.category.DEFAULT" />\
<data android:mimeType="application/com.example.nfctest/invoice" />
</intent-filter>
</activity>


thanks for the input


0 comments:

Post a Comment