Question

I'm working on a wikipedia reader where I want the user to be able to share the article he is reading to a friend using NFC. I dont want to be able to open these intents or anything fancy like that, just allow the friend to open up the url in the browser of his choice. I'm using a webview so getting ahold of the url won't be hard.

I've been searching the internet for some kind of example that is this easy but I haven't found anything simple enough. Do any of you have any recommendations for tutorials or examples?

Was it helpful?

Solution

It's really easy. To share something via Android Beam, you have to create a so-called NDEF message. An NDEF message contains one or more records, which have a specific type (e.g. URI, text, MIME type, etc.) and contain the data you want to share.

Add this piece of code somewhere in the Activity that shows the URL you want to share and make sure it is called whenever the URL changes:

NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);// (only need to do this once)
if (nfc != null) { // in case there is no NFC
  // create an NDEF message containing the current URL:
  NdefRecord rec = NdefRecord.createUri(url); // url: current URL (String or Uri)
  NdefMessage ndef = new NdefMessage(rec);
  // make it available via Android Beam:
  nfc.setNdefPushMessage(ndef);
}

OTHER TIPS

You should look at UriRecord or AbsoluteUriRecord, or even you can try an Android Application Record if you can find the one which launches the browser(?). And you should look into beam - there is a project boilerplate in the download sections here (shameless plug) :-) You might want to experiment with getting the desired effect using a tag first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top