代码拉取完成,页面将自动刷新
a base activity for use nfc
Maven
<dependency>
<groupId>xyz.leohan</groupId>
<artifactId>AndroidNFC</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
Gradle
compile 'xyz.leohan:AndroidNFC:1.0.0'
public class MyActivity extends NfcActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onNfcTouch() {
//this method will called when a NFC tag touched the phone and can be analysed
//we can get NFC tag id here;
}
}
<activity
android:name=".yourActivityName"
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
void readNfcContent();//can read message from NFC tag
boolean writeNfc(String msg);//write something to NFC tag
boolean deleteNfc(); //clear NFC tag
public class MainActivity extends NfcActivity {
private TextView tvContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvContent = (TextView) findViewById(R.id.tv_content);
}
@Override
protected void onNfcTouch() {
Log.i("nfc","ontouch");
tvContent.setText("NFC TagId:" + getTagId());
}
public void readNfc(View view) {
String s = null;
try {
//read nfc content from tag;
s = this.readNfcContent();
} catch (Exception e) {
e.printStackTrace();
}
tvContent.setText(s);
}
public void writeNfcContent(View view) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String content = dateFormat.format(System.currentTimeMillis());
try {
//write something to tag;
this.writeNfc(content);
} catch (Exception e) {
e.printStackTrace();
}
}
public void clearNfc(View view) {
try {
//clear nfcContent
this.deleteNfc();
} catch (Exception e) {
e.printStackTrace();
}
}
}
you can sended me an e-mail :leo@leohan.xyz
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。