Android 8.1系统重启后onNewIntent()失效的技术问题咨询
Android 8.1 Kiosk应用重启后onNewIntent()无法识别NFC标签问题
我在Android 8.1平板上开发了一款Kiosk应用,遇到以下问题:
- 系统重启后,应用中的
onNewIntent()无法识别NFC标签,但设备仍会发出NFC标签检测的提示音 - 执行
adb shell am start com.android.settings命令进入设置再返回应用,或关闭应用重新打开,onNewIntent()即可恢复正常 - 使用Android Studio重装应用后,
onNewIntent()也能正常工作 - 该问题仅在系统重启后出现
Android核心代码
@Override protected void onResume() { super.onResume(); NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter != null) { PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null); } } @Override protected void onPause() { super.onPause(); NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter != null) nfcAdapter.disableForegroundDispatch(this); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { Toast.makeText(this, "NFC TAG DISCOVERED.", Toast.LENGTH_LONG).show(); // ... } setIntent(intent); }
Kiosk模式配置方式
设置设备所有者的adb命令:
.\adb -s 192.168.2.170:5555 shell dpm set-device-owner com.crezzur.dienstplannerprodvx/.DeviceOwnerReceiver
对应的初始化代码:
private void setupDeviceOwner(Context context) { DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName adminComponentName = DeviceOwnerReceiver.getComponentName(this); if (dpm.isDeviceOwnerApp(getPackageName())) { //startLockTask(); dpm.setLockTaskPackages(adminComponentName, new String[]{ "com.crezzur.dienstplannerprodvx", "com.android.settings", }); dpm.setKeyguardDisabled(adminComponentName, true); dpm.setStatusBarDisabled(adminComponentName, true); } dpm.isDeviceOwnerApp(getPackageName()); }
内容的提问来源于stack exchange,提问作者user3877838
相关产品推荐
相关产品推荐

