ANDROID 一个app中有多个activity读取NFC标签问题

ANDROID 一个app中有多个activity读取NFC标签问题

最近在写有关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();
        //Enable forground dispatching to get the tags
        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)));
        }
    }
}

问题解决。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值