蔡华林
蔡华林
  • 发布:2019-03-21 08:30
  • 更新:2019-04-22 11:08
  • 阅读:1692

NFC newintent 新意图事件没作用

分类:Native.js
NFC

最近项目APP要读取NFC卡,我按照DCloud_Android_磊子提供的例子进行测试,每次读卡会触发pause和resume事件,newintent就是不触发,我的手机为AGM X3,有相关经验的同学请指出问题,谢谢。

我也尝试把读取的代码放到resume事件中,nfc数据也读取成功了,但出现个现象,每当我将APP转入后台然后重新返回APP界面,resume事件也触发了,并返回了上次NFC读取的数据,事件冲突了。代码如下:
function test_nfc() {
var Intent = plus.android.importClass('android.content.Intent');
var Activity = plus.android.importClass('android.app.Activity');
var PendingIntent = plus.android.importClass('android.app.PendingIntent');
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
var nfcAdapter = NfcAdapter.getDefaultAdapter(main);
if(!nfcAdapter){
return;
}
if (!nfcAdapter.isEnabled()) {
alert("请在系统设置中先启用NFC功能!");
return;
}
var main = plus.android.runtimeMainActivity();
var intent = new Intent(main, main.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
var pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
var ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
ndef.addDataType("/");
var intentFiltersArray = [ndef];
var techListsArray = [
["android.nfc.tech.IsoDep"],
["android.nfc.tech.NfcA"],
["android.nfc.tech.NfcB"],
["android.nfc.tech.NfcF"],
["android.nfc.tech.NfcV"],
["android.nfc.tech.Ndef"],
["android.nfc.tech.NdefFormatable"],
["android.nfc.tech.MifareClassic"],
["android.nfc.tech.MifareUltralight"]
];

document.addEventListener("newintent",function() {  
        console.log('newintent');  
    }, false);  

document.addEventListener("pause", function(e) {  
        console.log('pause');  
}, false);  

document.addEventListener("resume", function(e) {  
        console.log('resume');  
}, false);  

nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
}

2019-03-21 08:30 负责人:无 分享
已邀请:
2***@qq.com

2***@qq.com

我的项目也是跟您出现同样的问题,请问您解决了吗??????

2***@qq.com

2***@qq.com

我的项目也是跟您出现同样的问题,请问您解决了吗??????

2***@qq.com

2***@qq.com

我的项目也是跟您出现同样的问题,请问您解决了吗??????

  • 蔡华林 (作者)

    到现在为止没有解决

    2019-03-29 08:11

3***@qq.com

3***@qq.com

h5+估计是搞不定

该问题目前已经被锁定, 无法添加新回复