android项目实战 博学谷(Android项目实战-博学谷视频)

本文目录
Android项目实战-博学谷视频
《PHP5高级应用开发实践》,一个老外写的。Larry Ullman!我就在看这本书,写的不错,里面融入作者的部分开发经验,对于入门级别的菜鸟的水平提高有很大帮助!
Android NFC开发实战
对于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;
// record 0 contains the MIME type, record 1 is the AAR, if present
mInfoText.setText(new String(msg.getRecords().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, 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);
}
}
}

更多文章:
immediately的用法(immediately的含义及用法)
2026年4月28日 23:00
prepare for disappointment(disappointment是什么意思)
2025年12月26日 11:15
backspace用英文怎么读(Backspace,这个键怎么读)
2025年10月1日 12:45
javaweb实验心得(求一份java上机实验心得,300字左右)
2025年7月1日 05:45
fgets函数的作用是(标准函数fgets(s,n,f)的功能是)
2025年12月10日 18:30
settimeout类型(setTimeout()和setInterval()方法的区别)
2026年5月26日 18:30
prefs文件怎么打开(C4D如何导出自定义快捷键该文件后缀为res的格式)
2026年9月21日 22:30
什么是sophisticated(sophisticated 是什么意思)
2026年1月3日 14:15
struts sql结构(如何理解SQL servers的体系结构)
2025年11月3日 18:00
iferror函数参数使用(excel2010表格中如何使用iferror函数 iferror函数在excel中使用方法)
2026年4月26日 15:15
holders是什么意思(clear holders 什么意思)
2025年12月9日 21:00
linux下安装jdk解压就完了(linux下安装jdk并设置环境变量)
2025年12月22日 12:30
python基础编程代码(Python中的程序基本结构有哪些呢)
2026年9月3日 20:00









