Game2.tw提供最新手遊遊戲攻略,找攻略,就上Game2.tw

首頁 > Android > Android教程 >

Android NFC開發實戰

編輯:game2.tw
分享到:

  對於Android 4.0 SDK中提供的Beam例子,對於NFC開發來說的確是一個不錯的模板。對於瞭解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設備

  if (mNfcAdapter == null) {

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

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

  }

  

  mNfcAdapter.setNdefPushMessageCallback(this, this); //註冊NDEF回調消息

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

  }

  }

  }

熱門遊戲: 崩壞學園| 植物大戰殭屍2| 武俠Q傳| 神魔之塔| 遠的要命的王國| 部落戰爭| 曹操之野望| 戰神無雙| 釣魚發燒友| 一姬當千| 三國異聞錄.初章| 仙國志| 魔靈召喚: 天空之役| 攻城掠地手機版| 史上最坑爹的遊戲3| 忍者必須死2| 一個都不能死| 神魔之塔簡體騰訊版| 123猜猜猜台灣版| 成語大挑戰| 怪物x聯盟| 全民打棒球2013| 龍珠Q傳| 口袋戰姬| 瘋狂猜成語| 爐石戰記:魔獸英雄傳| 愛新覺羅| LINE釣魚大師| 魅子online| 勇者前線 BraveFrontier| 真三國大戰| 召喚圖板 サモンズボード| 放開那三國| 愛養成2| Line Rangers| Boom Beach| 巨砲連隊| 鬼武傳| 戰姬天下| 幻想の英雄| 暗黑戰神| 神之刃| COOKIE RUN 跑跑薑餅人| 猜猜巧克力| 神鬼幻想| 神鵰俠侶| 卡通農場 Hay day| LINE Pokopang 波兔村保衛戰| 秦時明月| 坑爹的遊戲2| 我是火影| 龍之力量| 城堡爭霸 - Castle Clash| 海賊大亂鬥| Ace Fishing 釣魚發燒友| Chain Chronicle 鎖鏈戰記|

Game2.tw遊戲攻略、資訊網站
申請友情鏈接,申請遊戲專區建立,發放遊戲活動碼,請聯繫bd#game2.tw(#替換成@)