如何在android中无需刷新即可进行NFC扫描?
我正在尝试在android中添加NFC标签。 “我的活动”在onCreate()中有一个webview及其设置的url。 NFC标记时,可以正常获取NDEF信息,但不维护页面地址 我怎样才能解决它如何在android中无需刷新即可进行NFC扫描?,android,webview,tags,nfc,ndef,Android,Webview,Tags,Nfc,Ndef,我正在尝试在android中添加NFC标签。 “我的活动”在onCreate()中有一个webview及其设置的url。 NFC标记时,可以正常获取NDEF信息,但不维护页面地址 我怎样才能解决它 @Override public void onCreate(Bundle savedInstanceBundle){ super.onCreate(savedInstanceBundle); setContentView(R.layout.activity_test); th
@Override
public void onCreate(Bundle savedInstanceBundle){
super.onCreate(savedInstanceBundle);
setContentView(R.layout.activity_test);
this.webview = findViewbyId(R.id.webview);
this.webview.loadUrl("https://www.abcblarblar.com/");
this.nfcAdapter = NfcAdapter.getDefaultAdapter(this);
}
@Override
public void onResume(){
super.onResume();
Intent intent = getIntent();
if (NfcAdapter.ACTION_TAG_DISCOVERED == intent.getAction()) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
...
}
}
manifest.xml文件
<activity
android:name="com.xxx.xxx.xxx.NFCActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
我猜(因为您没有显示有关如何读取NFC标签的代码),您正在使用enableForegroundDispatch
此方法在Android操作系统读取NFC卡时暂停应用程序
如果这是原因,有两种可能的解决方案:-
1) 确保您的应用程序处理暂停并保存其InstanceState(因为它有可能被终止并重新启动),或者您正在执行导致此问题的onResume
2) 使用
enableReaderMode
读取卡,因为通过这种读取NFC卡的方式,您的应用程序不会暂停,而是在应用程序中的单独线程中创建回调precurve@Andrew I write enableForegroundDispatch(),并确认URL保持不变。