• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

AndroidNFC: a base activity for use nfc

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

AndroidNFC

开源软件地址:

https://gitee.com/leohan1992/AndroidNFC

开源软件介绍:

AndroidNFC

a base activity for use nfc

dependency

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'

How to Use

  1. create a New Activity extends xyz.leohan.androidnfclib.NfcActivity,implement onNfcTouch() method.Then do other things in an Activit as usual
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;    }}
  1. write these in your AndroidManifest.xml.
    android:launchMode="singleTask" and the intent-filter is necessary
       <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>
  1. main methods in NFCActivity:
void readNfcContent();//can read message from NFC tagboolean writeNfc(String msg);//write something to NFC tagboolean deleteNfc(); //clear NFC tag

Sample

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();        }    }}
  1. I already checked the Runtime Permission in library. Don't worry about it.

Contact Me

you can sended me an e-mail :[email protected]


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap