Reputation: 2616
I used this code for integrating NFC
reading in my android
application. Write a plain text to the NFC
tag and read using app it is perfectly working. Now my requirement is to read URL
from the NFC
tag.When reading value from NFC
tag it automatically open the browser and loading the URL
.So what changes needed to achieve the reading content and open my app?
Upvotes: 2
Views: 2440
Reputation: 2654
You can use filter if you want to start the app when get closer to NFC tag, but be aware if your app is running it won't be notified about the tag. You have to register it inside your app:
protected void onCreate(Bundle savedInstanceState) {
...
Intent nfcIntent = new Intent(this, getClass());
nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
nfcPendingIntent =
PendingIntent.getActivity(this, 0, nfcIntent, 0);
IntentFilter tagIntentFilter =
new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
tagIntentFilter.addDataType("text/plain");
intentFiltersArray = new IntentFilter[]{tagIntentFilter};
}
catch (Throwable t) {
t.printStackTrace();
}
}
and remember to enable it in onResume:
nfcAdpt.enableForegroundDispatch(
this,
nfcPendingIntent,
intentFiltersArray,
null);
handleIntent(getIntent());
and unregister it in onPause:
nfcAdpt.disableForegroundDispatch(this);
..be aware that the data can be stored in SmartPoster structure in your NFC tag. In this case you have to read it in another way. In my blog you can find a post about reading SmartPoster and more
Upvotes: 0
Reputation: 70
Add to your Manifest
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<data
android:host="your host name"
android:scheme="http" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
in the activity you want to open
Upvotes: 1
Reputation: 1372
Here I assume that results returned will be only url and no other data with it, so just modify onPostExecute
as :
@Override
protected void onPostExecute(String result) {
if (result != null) {
String url = result;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
}
If other data also included than parse results to get only URL.
Upvotes: 0