編輯:Android手機系統教程
對於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);
}
}
}
手機怎麼看安卓系統版本手機怎麼看安卓系統版本在安卓手機上直接查看安卓版本:進入手機系統“設置”--找到“關於手機”並進入,之後在裡面即可看到了,如下圖:
華為華為mate9再一次將手機攝影變成了一件非常非常享受的事情。華為華為mate9配備了一顆1200萬像素的RGB攝像頭和一顆2000萬像素的單色攝像頭,在拍照的過
華碩ZenFone2截屏方法1:同時按住【電源鍵】+【音量下鍵】。 華碩ZenFone2截屏方法2: 1)打開華碩ZenFone2【設置】點擊【asus個人化設
水貨手機困擾廣大購機朋友的難題,稍不注意就被不良商販所蒙騙,花了行貨的錢卻買了水貨的產品,下面我們就來看看如何分辨行貨手機與水貨手機的區別: 第一,從“進網許可”