本文整理匯總了Java中android.nfc.NdefMessage類的典型用法代碼示例。如果您正苦於以下問題:Java NdefMessage類的具體用法?Java NdefMessage怎麽用?Java NdefMessage使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
NdefMessage類屬於android.nfc包,在下文中一共展示了NdefMessage類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: readNfcTag
import android.nfc.NdefMessage; //導入依賴的package包/類
/**
* 讀取NFC標簽文本數據
*/
private void readNfcTag(Intent intent) {
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msgs[] = null;
int contentSize = 0;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
contentSize += msgs[i].toByteArray().length;
}
}
try {
if (msgs != null) {
NdefRecord record = msgs[0].getRecords()[0];
String textRecord = parseTextRecord(record);
mTagText += textRecord + "\n\ntext\n" + contentSize + " bytes";
}
} catch (Exception e) {
}
}
}
示例2: onNewIntent
import android.nfc.NdefMessage; //導入依賴的package包/類
@Override
public void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
if(intent.hasExtra(NfcAdapter.EXTRA_TAG ))
{
if(BackgroundTask.isNetworkAvailable(HubMain.this))
{
gifImageView.setVisibility(View.INVISIBLE);
spinner.setVisibility(View.VISIBLE);
}
Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if(parcelables != null && parcelables.length > 0)
{
readTextfromMessage((NdefMessage)parcelables[0]);
}
}
}
示例3: tryUpdateIntentFromBeam
import android.nfc.NdefMessage; //導入依賴的package包/類
/**
* Checks to see if the activity's intent ({@link android.app.Activity#getIntent()}) is
* an NFC intent that the app recognizes. If it is, then parse the NFC message and set the
* activity's intent (using {@link Activity#setIntent(android.content.Intent)}) to something
* the app can recognize (i.e. a normal {@link Intent#ACTION_VIEW} intent).
*/
public static void tryUpdateIntentFromBeam(Activity activity) {
Intent originalIntent = activity.getIntent();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(originalIntent.getAction())) {
Parcelable[] rawMsgs = originalIntent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) rawMsgs[0];
// Record 0 contains the MIME type, record 1 is the AAR, if present.
// In iosched, AARs are not present.
NdefRecord mimeRecord = msg.getRecords()[0];
if (ScheduleContract.makeContentItemType(
ScheduleContract.Sessions.CONTENT_TYPE_ID).equals(
new String(mimeRecord.getType()))) {
// Re-set the activity's intent to one that represents session details.
Intent sessionDetailIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(new String(mimeRecord.getPayload())));
activity.setIntent(sessionDetailIntent);
}
}
}
示例4: handleIntent
import android.nfc.NdefMessage; //導入依賴的package包/類
private void handleIntent(final Intent intent) {
final String action = intent.getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
final String inputType = intent.getType();
final NdefMessage ndefMessage = (NdefMessage) intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
final byte[] input = Nfc.extractMimePayload(Constants.MIMETYPE_TRANSACTION, ndefMessage);
new BinaryInputParser(inputType, input) {
@Override
protected void handlePaymentIntent(final PaymentIntent paymentIntent) {
cannotClassify(inputType);
}
@Override
protected void error(final int messageResId, final Object... messageArgs) {
dialog(WalletActivity.this, null, 0, messageResId, messageArgs);
}
}.parse();
}
}
示例5: processIntent
import android.nfc.NdefMessage; //導入依賴的package包/類
private void processIntent(Intent i) {
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
Parcelable[] rawMsgs =
i.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) rawMsgs[0];
String url = new String(msg.getRecords()[0].getPayload());
Utils.debugLog(TAG, "Got this URL: " + url);
Toast.makeText(this, "Got this URL: " + url, Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setClass(this, ManageReposActivity.class);
startActivity(intent);
finish();
}
}
示例6: formatTag
import android.nfc.NdefMessage; //導入依賴的package包/類
private void formatTag(Tag tag, NdefMessage ndefMessage)
{
try
{
NdefFormatable ndefFormatable = NdefFormatable.get(tag);
if (ndefFormatable == null)
{
Toast.makeText(this, "Tag is not formatable", Toast.LENGTH_LONG).show();
}
ndefFormatable.connect();
ndefFormatable.format(ndefMessage);
ndefFormatable.close();
}
catch (Exception e)
{
Log.e("formatTag", e.getMessage());
}
}
示例7: onNewIntent
import android.nfc.NdefMessage; //導入依賴的package包/類
public void onNewIntent(Intent intent) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{createUriRecord(mUri)});
boolean result = writeTag(ndefMessage, detectedTag);
if (result) {
Toast.makeText(this, "寫入成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "寫入失敗", Toast.LENGTH_SHORT).show();
}
}
示例8: writeTag
import android.nfc.NdefMessage; //導入依賴的package包/類
/**
* 寫入標簽
*
* @param message
* @param tag
* @return
*/
public static boolean writeTag(NdefMessage message, Tag tag) {
int size = message.toByteArray().length;
try {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
return false;
}
if (ndef.getMaxSize() < size) {
return false;
}
ndef.writeNdefMessage(message);
return true;
}
} catch (Exception e) {
}
return false;
}
示例9: createNdefMessage
import android.nfc.NdefMessage; //導入依賴的package包/類
/**
* Implementation for the CreateNdefMessageCallback interface
*/
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
Time time = new Time();
time.setToNow();
String text = ("Beam me up!\n\n" +
"Beam Time: " + time.format("%H:%M:%S"));
NdefMessage msg = new NdefMessage(NdefRecord.createMime(
"application/com.example.android.beam", text.getBytes())
/**
* The Android Application Record (AAR) is commented out. When a device
* receives a push with an AAR in it, the application specified in the AAR
* is guaranteed to run. The AAR overrides the tag dispatch system.
* You can add it back in to guarantee that this
* activity starts when receiving a beamed message. For now, this code
* uses the tag dispatch system.
*/
//,NdefRecord.createApplicationRecord("com.example.android.beam")
);
return msg;
}
示例10: processIntent
import android.nfc.NdefMessage; //導入依賴的package包/類
/**
* Parses the NDEF Message from the intent and prints to the TextView
*/
void processIntent(Intent intent) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0];
// record 0 contains the MIME type, record 1 is the AAR, if present
mInfoText.setText(new String(msg.getRecords()[0].getPayload()));
}
示例11: readNFCTag
import android.nfc.NdefMessage; //導入依賴的package包/類
private String readNFCTag(Intent intent){
String response=null;
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())){
//從標簽讀取數據
Parcelable[] nfcMsgs=intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES
);
NdefMessage msgs[]=null;
int contentSize=0;
if(nfcMsgs!=null){
msgs=new NdefMessage[nfcMsgs.length];
// 標簽可能存儲多個NdefMessage對象,一般情況下隻有一個
for(int i=0;i<nfcMsgs.length;i++){
// 轉換為NdefMessage對象
msgs[i]= (NdefMessage) nfcMsgs[i];
// 計算數據的總長度
contentSize+=msgs[i].toByteArray().length;
}
}
try{
if(msgs!=null){
// 隻讀第一個信息
NdefRecord record=msgs[0].getRecords()[0];
response=parseTextRecord(record);
System.out.println(response);
}
}
catch (Exception e){
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}
return response;
}
示例12: setBeamSessionUri
import android.nfc.NdefMessage; //導入依賴的package包/類
/**
* Sets this activity's Android Beam message to one representing the given session.
*/
public static void setBeamSessionUri(Activity activity, Uri sessionUri) {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
if (nfcAdapter == null) {
// No NFC :-(
return;
}
nfcAdapter.setNdefPushMessage(new NdefMessage(
new NdefRecord[]{
new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
ScheduleContract.makeContentItemType(
ScheduleContract.Sessions.CONTENT_TYPE_ID).getBytes(),
new byte[0],
sessionUri.toString().getBytes())
}), activity);
}
示例13: eraseTag
import android.nfc.NdefMessage; //導入依賴的package包/類
private void eraseTag(CallbackContext callbackContext) throws JSONException {
Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefRecord[] records = {
new NdefRecord(NdefRecord.TNF_EMPTY, new byte[0], new byte[0], new byte[0])
};
writeNdefMessage(new NdefMessage(records), tag, callbackContext);
}
示例14: onCreate
import android.nfc.NdefMessage; //導入依賴的package包/類
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cinemas = DBHelper.getInstance(getContext()).getCinemas();
watcher = new Gson().fromJson(getArguments().getString("watcher"), Watcher.class);
nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
if(nfcAdapter != null) {
nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
@Override
public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
return new NdefMessage(
new NdefRecord[] {
NdefRecord.createUri(BuildConfig.SERVER_BASE_URL + "w/" + watcher.getID()),
NdefRecord.createApplicationRecord(BuildConfig.APPLICATION_ID)
}
);
}
}, getActivity());
}
}
示例15: write
import android.nfc.NdefMessage; //導入依賴的package包/類
/**
* Write text to a tag
*
* @param textToWrite the text to write
*/
public void write(String textToWrite) {
Locale locale = Locale.US;
final byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("UTF-8"));
final byte[] textBytes = textToWrite.getBytes(Charset.forName("UTF-8"));
final int utfBit = 0;
final char status = (char) (utfBit + langBytes.length);
final byte[] data = new byte[1 + langBytes.length + textBytes.length];
data[0] = (byte) status;
System.arraycopy(langBytes, 0, data, 1, langBytes.length);
System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
NdefRecord[] records = {record};
messageToWrite = new NdefMessage(records);
}