问题
I want to know if it is possible to do the following;
I want to have a nfc tag that works in conjunction with an Android application that I am developing.
If i swipe the tag and my application is installed on the phone I want to read of plain text data from the tag. Nothing fancy just a number up to 999.
But if my application is not installed I want the tag to open up the web browser and redirect to a link where they can download this?
Is this possible with the way android have gone about reading the intents?
from my understanding of it (im no pro at nfc) you can only do one of them?
Or am I wrong?
Edit:
is there any way to do it without loading the application on play store? It is for a university project and I don't think that they will be happy if i make the project publicly available. i was hoping to just take them to a link where you can download the .apk
回答1:
If avoiding Play Store is a requirement, you can store an NDEF message containing two records on the tag: The first record contains the URL to your app's download page. The second record contains the plain text data. (In fact, you could even encode the data into the URL and, therefore use only one URI record.)
+----------------------------------------------+------------------+ | URI Record: http://www.zapnologica.com/myapp | Text Record: nnn | +----------------------------------------------+------------------+
If your application is not installed, the app's download page (here: http://www.zapnologica.com/myapp) will be opened in the user's web browser.
In order to automatically launch your app upon tag scanning, your app needs to define an intent filter for the download URL:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="www.zapnologica.com"
android:pathPrefix="/myapp" />
</intent-filter>
Then, within your app, you can read the whole NDEF message from the tag (actually the NDEF message will be provided to your app in the intent extra EXTRA_NDEF_MESSAGES
automatically):
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
Parcelable[] rawMsgs = Intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
}
Thus, msg[0].getRecords()
will return two records -- the URI record and the Text record.
回答2:
I think that this is what you are looking for:
Tutorial NFC.
If a user without our app touches the tag, it’ll use a built in mechanism to take them to the download page for the app in the Android Play Store. Once installed any subsequent taps will launch the app and show the game card on screen.
来源:https://stackoverflow.com/questions/19402435/two-actions-on-one-nfc-tag