本文整理汇总了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);
}