我有一个应用程序,我已设置为设备所有者并开始进入自助服务终端模式。这是有效的,因为按下后退按钮,主页或最近的应用程序按钮不允许用户离开应用程序除了一个用例:
我注意到如果用户按下最近的应用按钮,则切换屏幕(电源按钮两次),然后显示默认的Android启动器,而不是加载回应用程序。在按下电源按钮之前,应用程序未关闭。我已经考虑将应用程序设置为我的HOME屏幕,但这会导致我的应用程序出现问题,因为它连接到BLE设备,这导致我认为应用程序的两个实例正在运行(一个在启动时启动,然后一个新的开始为家庭)。对于这两个实例,现在在后台的实例保持在BLE连接上,因此启动了HOME屏幕的连接。 log cat也显示没有错误或应用程序崩溃。
以下是应用行为的视频。正如你可以看到击中主页,最近的应用程序或后退按钮没有关闭应用程序,直到我点击最近的应用程序+电源按钮: https://www.youtube.com/watch?v=-3f1Jp2fw6g
MainActivity.java 公共类MainActivity扩展了AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
BluetoothHelper mBleHelper;
DeviceController mDeviceController;
Context mContext = this;
Activity mActivity = this;
private int unlockKioskCount = 0;
private long unlockKioskTime=0;
private BluetoothHelper.BluetoothStateChangedCallback mBluetoothStateChanged = new BluetoothHelper.BluetoothStateChangedCallback() {
@Override
public void onStateChanged(int state) {
onStateChangeFragmentCheck();
}
};
private DeviceController.DeviceStatusChangeListener mDeviceStatusChange = new DeviceController.DeviceStatusChangeListener() {
@Override
public void onChange(int state) {
onStateChangeFragmentCheck();
}
};
private DeviceController.DataDownloadedListener mDataDownloadedStatusChange = new DeviceController.DataDownloadedListener() {
@Override
public void onDownloaded() {
onStateChangeFragmentCheck();
}
};
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case BluetoothHelper.REQUEST_ENABLE_BT: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
onStateChangeFragmentCheck();
}
break;
}
}
}
private void onStateChangeFragmentCheck() {
if (mBleHelper.needsSetupStep()) {
BaseFragment.showSetupFragment(mContext, mBleHelper);
} else if (BaseFragment.isSetupStepShowing(mContext)) {
BaseFragment.startFragment(mContext, new ControlFragment());
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Make it full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mDeviceController = new DeviceController(this, mDeviceStatusChange);
mBleHelper = new BluetoothHelper(this, mDeviceController, mBluetoothStateChanged);
mDeviceController.setDataDownloadedListener(mDataDownloadedStatusChange);
onStateChangeFragmentCheck();
// Setup a battery view
new BatteryView((TextView) findViewById(R.id.battery_level), this);
}
public DeviceController getDeviceController() {
return mDeviceController;
}
private void lockDevice() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// get policy manager
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// get this app package name
ComponentName mDPM = new ComponentName(this, KioskModeReceiver.class);
if (myDevicePolicyManager != null && myDevicePolicyManager.isDeviceOwnerApp(this.getPackageName())) {
// get this app package name
String[] packages = {this.getPackageName()};
// mDPM is the admin package, and allow the specified packages to lock task
myDevicePolicyManager.setLockTaskPackages(mDPM, packages);
startLockTask();
Log.d(TAG, "Device owner so pinned app");
muteDevice();
} else {
Log.d(TAG, "Not a device owner so app cant be pinned");
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
mBleHelper.unregisterBroadCastReceiver();
}
@Override
public void onResume() {
super.onResume();
mDeviceController.registerGattReceiver(this);
lockDevice();
findViewById(R.id.clock).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
long time= System.currentTimeMillis();
if (unlockKioskTime==0 || (time-unlockKioskTime> 3000) ) {
unlockKioskTime=time;
unlockKioskCount=1;
} else{
unlockKioskCount++;
}
if (unlockKioskCount==8) {
DeveloperDialog dialog = new DeveloperDialog(mActivity);
dialog.show();
}
}
});
}
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
super.onBackPressed();
}
}
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do something or nothing in your case
return true;
}
return super.onKeyLongPress(keyCode, event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return true;
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
return true;
}
if(keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.onKeyDown(keyCode, event);
}
private void muteDevice() {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am != null) {
am.setStreamVolume(AudioManager.STREAM_SYSTEM, 0, 0);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
}
}
有没有其他人看过像这样奇怪的用例?正在运行的手机是ZTE maxx pro。