Search

与我们合作

我们专注:网站策划设计、网络多媒体传播、网站优化及网站营销、品牌策略与设计
主营业务:网站建设、移动端微信小程序开发、VI设计、网络运营、云产品·运维解决方案

有一个品牌项目想和我们谈谈吗?

您可以填写右边的表格,让我们了解您的项目需求,这是一个良好的开始,我们将会尽快与您取得联系。当然也欢迎您给我们写信或是打电话,让我们听到您的声音

您也可通过下列途径与我们取得联系:

地 址: 深圳.龙岗区大运软件小镇11栋3楼

电 话: 138 2888 4598 / 138 0880 9053

网 址: http://www.appvx.cn

快速提交您的需求 ↓

app有多个activity怎么读取NFC标签问题

时间:2017-09-30 浏览:4934 编辑:深正互联 来源:互联网

最近在写有关NFC标签读取录入的android APP。由于在AndroidManifest.xml配置使得NFC可以自启动对应activity,但APP中好多activity都需要NFC标签进行读取,这时activity的启动就出现了问题。 

最初代码如下

activity中获取NFC标签的id号:

    @Override

    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);

        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())// NDEF类型

        || NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())// 其他类型

        ||NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {            Toast.makeText(this,byteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)), Toast.LENGTH_LONG).show();

        }

    }

AndroidManifest中配置:

<activity android:name="" android:theme="@style/Theme.AppCompat.Light.NoActionBar"  android:launchMode="singleInstance">

            <intent-filter>

                <action android:name="android.nfc.action.TECH_DISCOVERED" />

            </intent-filter>

            <intent-filter>

                <action android:name="android.nfc.action.TAG_DISCOVERED" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

            <meta-data

                android:name="android.nfc.action.TECH_DISCOVERED"

                android:resource="@xml/nfc_tech_filter" />

        </activity>

新建nfc_tech_filter.xml配置:

<?xml version="1.0" encoding="utf-8"?>

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

    <tech-list>

        <tech>android.nfc.tech.IsoDep</tech>

    </tech-list>

    <tech-list>

        <tech>android.nfc.tech.NfcV</tech>

    </tech-list>

    <tech-list>

        <tech>android.nfc.tech.NfcF</tech>

    </tech-list>

</resources>

到此就可以获得NFC标签的ID了,但问题是我好多activity配置完后每次刷NFC卡总是会将在AndroidManifest中配置的第一个带有NFC配置的activity打开,而不是在当前activity读取NFC标签。之后做了如下修改,将次问题解决:

1.删掉AndroidManifest对应activity配置:

2.将xxxx activity写为:

public class xxxx  extends Activity {

    private NfcAdapter mAdapter;

    private PendingIntent mPendingIntent;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.xxxxx);

        mAdapter = NfcAdapter.getDefaultAdapter(this);

        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    }

    @Override

    protected void onResume() {

        super.onResume();

        mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); //ended up setting mFilters and mTechLists to null

    }

    @Override

    public void onPause() {

        super.onPause();

        //You need to disable forgroundDispatching here

        mAdapter.disableForegroundDispatch(this);

    }

    @Override

    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);

        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())// NDEF类型

        || NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())// 其他类型

        ||NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {           cardIdEditText.setText(byteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));

        }

    }

}


本站文章均为深正网站建设摘自权威资料,书籍,或网络原创文章,如有版权纠纷或者违规问题,请即刻联系我们删除,我们欢迎您分享,引用和转载,但谢绝直接搬砖和抄袭!感谢...
GO 欣赏案例
查看经典案例
在线咨询
电话咨询
0755-36630177
返回顶部
在线咨询 获取报价 电话咨询
获取报价
您的称呼:

*

您的电话:

*

您的邮箱:

*

提交 重置
重要的事情,电话里聊

接通客服

不方便的时候线上咨询,在线等哦