一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Android NFC開發(fā)實戰(zhàn)

 獨孤求財 2013-05-28

Android NFC開發(fā)實戰(zhàn)

9
出處:Android開發(fā)網(wǎng) 時間:2012-02-22 人氣:3159

核心提示:本教程為大家介紹android開發(fā)中NFC實例。

  對于Android 4.0 SDK中提供的Beam例子,對于NFC開發(fā)來說的確是一個不錯的模板。對于了解NFC的NDEF消息處理過程不妨看下面的代碼。

  public class Beam extends Activity implements CreateNdefMessageCallback,

  OnNdefPushCompleteCallback {

  NfcAdapter mNfcAdapter;

  TextView mInfoText;

  private static final int MESSAGE_SENT = 1;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  mInfoText = (TextView) findViewById(R.id.textView);

  mNfcAdapter = NfcAdapter.getDefaultAdapter(this); //實例化NFC設(shè)備

  if (mNfcAdapter == null) {

  mInfoText = (TextView) findViewById(R.id.textView);

  mInfoText.setText("NFC is not available on this device.");

  }

  

  mNfcAdapter.setNdefPushMessageCallback(this, this); //注冊NDEF回調(diào)消息

  mNfcAdapter.setOnNdefPushCompleteCallback(this, this);

  }

  

  @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(

  new NdefRecord[] { createMimeRecord(

  "application/com.example.android.beam", text.getBytes())

  });

  return msg;

  }

  @Override

  public void onNdefPushComplete(NfcEvent arg0) {

  // A handler is needed to send messages to the activity when this

  // callback occurs, because it happens from a binder thread

  mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();

  }

  private final Handler mHandler = new Handler() {

  @Override

  public void handleMessage(Message msg) {

  switch (msg.what) {

  case MESSAGE_SENT:

  Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();

  break;

  }

  }

  };

  @Override

  public void onResume() {

  super.onResume();

  if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {

  processIntent(getIntent());

  }

  }

  @Override

  public void onNewIntent(Intent intent) {

  // onResume gets called after this to handle the intent

  setIntent(intent);

  }

  /**

  * 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()));

  }

  /**

  * Creates a custom MIME type encapsulated in an NDEF record

  *

  * @param mimeType

  */

  public NdefRecord createMimeRecord(String mimeType, byte[] payload) {

  byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));

  NdefRecord mimeRecord = new NdefRecord(

  NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);

  return mimeRecord;

  }

  @Override

  public boolean onCreateOptionsMenu(Menu menu) {

  // If NFC is not available, we won't be needing this menu

  if (mNfcAdapter == null) {

  return super.onCreateOptionsMenu(menu);

  }

  MenuInflater inflater = getMenuInflater();

  inflater.inflate(R.menu.options, menu);

  return true;

  }

  @Override

  public boolean onOptionsItemSelected(MenuItem item) {

  switch (item.getItemId()) {

  case R.id.menu_settings:

  Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);

  startActivity(intent);

  return true;

  default:

  return super.onOptionsItemSelected(item);

  }

  }

  }

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    欧美极品欧美精品欧美| 亚洲欧美黑人一区二区| 亚洲妇女作爱一区二区三区| 亚洲第一视频少妇人妻系列| 国产精品午夜福利免费阅读| 中文字幕有码视频熟女| 亚洲综合一区二区三区在线| 三级高清有码在线观看| 一区二区三区免费公开| 丝袜美女诱惑在线观看| 精品欧美日韩一区二区三区| 三级高清有码在线观看| 成人日韩在线播放视频| 欧洲亚洲精品自拍偷拍| 欧美一区二区三区性视频| 日韩一区二区免费在线观看| 日韩性生活片免费观看| 日本不卡在线视频你懂的| 精品国产成人av一区二区三区| 国产精品美女午夜福利| 在线日本不卡一区二区| 亚洲超碰成人天堂涩涩| 欧美成人精品一区二区久久| 91人妻人人揉人人澡人| 91福利视频日本免费看看| 日韩一级毛一欧美一级乱| 欧美日韩欧美国产另类| 国产爆操白丝美女在线观看| 成人三级视频在线观看不卡| 日本精品中文字幕人妻| 91偷拍与自偷拍精品| 国产又猛又黄又粗又爽无遮挡| 久久成人国产欧美精品一区二区| 日本成人三级在线播放| 日韩一区二区三区四区乱码视频| 熟妇人妻av中文字幕老熟妇| 极品少妇嫩草视频在线观看| 亚洲专区中文字幕视频| 国产一级性生活录像片| 91精品国自产拍老熟女露脸| 亚洲精品一区二区三区免|