I am working on Near Field Communication for reading data from NFC Tags. I don't have NFC supported Android Mobile and NFC Tags to test the Application i created .
I want to know whether it is possible to Launch my App through intent filter(Should assume NFC tag is detected from my device)
My Manifest Snippet :
<activity
android:name=".ServerActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="abc.com"
android:pathPrefix="/aaap"
android:scheme="http" />
</intent-filter>
</activity>
My Activity Snippet :
@Override
protected void onNewIntent(Intent intent) {
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Toast.makeText(getApplicationContext(), "ACTION_TAG_DISCOVERED",
Toast.LENGTH_LONG);
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
}
I may be completely missing the point here, but from what I understood, you basically want to fire off an intent that will get picked by the receiver you declared in your manifest, right? Have you tried setting up a very simple test app that does exactly this using the sendBroadcast(...)
method?
Intent exampleIntent = new Intent("android.nfc.action.NDEF_DISCOVERED");
sendBroadcast(exampleIntent)
You can add any extra data to the Intent
per your requirements.
After some research on NFC i found that we can read/write NFC Tags without NFC enabled device.. But answer is so simple.. It's nothing but playing with Intents :
We have to call this snippet for invoking NFC :
final Intent intent = new Intent(NfcAdapter.ACTION_TAG_DISCOVERED);
intent.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, "Custom Messages");
startActivity(intent);
Also we should register our Activity to Android System through Manifest file like below :
Manifest.xml
<activity
android:name="TagViewer" >
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Same way if you have more than one Activity using same action we will be getting chooser from which we can launch our Desired Activity.
Receiving inside Activity :
TagViewer .java
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
Log.v("NFC Launched","NFC Launched");
}
We have this sample in Developer Sample Demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With