我正在尝试在android中进行NFC标记。我的活动有一个webview及其在onCreate()中的设置URL。当进行NFC标记时,可以正常获取NDEF信息,但不保留页面地址。
我该如何解决?
@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
https://developer.android.com/reference/android/nfc/NfcAdapter#enableReaderMode(android.app.Activity,%20android.nfc.NfcAdapter.ReaderCallback,%20int,%20android.os.Bundle)来读取卡,因为用这种方式读取NFC卡不会使您的应用程序暂停,而是在您的应用程序中的单独线程中创建了回调