android - 如何禁用导航栏和状态栏的按钮?
问题描述
我正在尝试开发全屏模式应用程序。现在我已将我的应用程序设置为沉浸式模式,每次状态栏和导航栏都隐藏,并且仅在用户在屏幕的顶部/底部边缘做出滑动手势时显示它们
我认为不可能永久禁用状态栏和导航栏,但如果我隐藏为沉浸式模式并禁用导航栏的所有三个按钮(主页按钮、后退按钮和任务管理器按钮),尽管它们正在显示,这将是一个很好的解决方案
注意:我只为 Android P 开发这个应用程序,我只需要这个版本的解决方案。
要将应用程序设置为沉浸式模式,我已添加到我的AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
在我的MainActivity.kt中:
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_INMERSIVE_STICKY)
我一直在尝试访问 UI 侦听器以避免手势但失败了
window.decorView.setOnSystemUiVisibilityChangeListener {
//For example hide action
actionBar.hide()
}
而且我不知道如何访问导航栏按钮并禁用它们的操作,我也想避免从顶部边缘滑动并显示通知管理器视图。
请一些帮助和建议?提前致谢!
2019 年 9 月 18 日更新
为此,我在我的应用程序中实现了信息亭模式。
我创建了一个启动器活动,配置了应用程序所需的所有内容,如果一切顺利,应用程序运行,但如果没有,我会显示一条消息警告。
有两个前提条件非常重要:
1.设备中不应有任何账户(谷歌账户、三星账户等)
2.应用程序应该是设备的所有者和管理员(我稍后会解释)
脚步
1.在AndroidManifest中实现receiver
<receiver
android:name=".security.CustomDeviceAdminReceiver"
android:label="@string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_receiver" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
2.实现CustomDeviceAdminReceiver
class CustomDeviceAdminReceiver: DeviceAdminReceiver() {
companion object {
fun getComponentName(context: Context): ComponentName {
return ComponentName(context.applicationContext, CustomDeviceAdminReceiver::class.java)
}
}
}
3. 创建 xml 文件夹并在其中创建一个名为 device_admin_receiver 的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<device-admin>
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
<expire-password />
<encrypted-storage />
<disable-camera />
</uses-policies>
</device-admin>
4. 在启动器活动中执行以下操作:
class LauncherActivity : BaseActivity() {
private var adminComponentName: ComponentName
private var devicePolicyManager: DevicePolicyManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.launcher_activity)
adminComponentName = CustomDeviceAdminReceiver.getComponentName(this)
devicePolicyManager = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
}
override fun onResume() {
super.onResume()
initKioskMode()
}
private fun initKioskMode() {
setUserRestriction(UserManager.DISALLOW_SAFE_BOOT)
setUserRestriction(UserManager.DISALLOW_FACTORY_RESET)
setUserRestriction(UserManager.DISALLOW_ADD_USER)
setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA)
setKeyGuardEnabled()
}
private fun setUserRestriction(restriction: String) = devicePolicyManager.addUserRestriction(adminComponentName, restriction)
private fun setKeyGuardEnabled() {
devicePolicyManager.setKeyguardDisabled(adminComponentName, false)
setLockTask()
}
private fun setLockTask() {
devicePolicyManager.setLockTaskPackages(adminComponentName, arrayOf(packageName))
startLockTask()
startActivity(Intent(this, MainActivity::class.java))
}
private fun checkAppDeviceOwnerStatus() {
if (isAppDeviceOwner() && isAppDeviceActiveAdmin()) initKioskMode()
else {
stopLockTask()
showWarningAdvices()
}
}
private fun isAppDeviceOwner() = devicePolicyManager.isDeviceOwnerApp(packageName)
private fun isAppDeviceActiveAdmin() = devicePolicyManager.isAdminActive(adminComponentName)
private fun showWarningAdvices() {
adminWarningTitle.visibility = View.VISIBLE
adminWarningMessage.visibility = View.VISIBLE
}
}
5. 将我的 Activity 设为 Main 和 Launcher
<activity
android:name=".screen.main.LauncherActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
6. 最后,您必须将您的应用设置为设备所有者和管理设备应用
这是通过命令行使用 ->
adb shell dpm set-device-owner packageName/path to Admin device file
PD:要从设备中删除必须在终端中运行的所有者配置文件:
adb shell dpm remove-active-admin packageName/path to Admin device file
啊啊啊就是这样!!
解决方案
推荐阅读
- python - 计算 Pandas 到期的先进先出库存
- excel - Applescript将图像从我桌面上的文件夹复制到excel列A
- markdown - 在 Colab 的 Markdown 中渲染内联图像
- java - 加载加密密钥
- javascript - 制作一个看起来像两个的 SVG 路径,而仍然是一个用于动画的路径
- arangodb-foxx - 有没有办法在 foxxservice 上获得与在 arangosh 上相同的输出?
- excel - Excel 堆栈排名多个现有排名
- graphql - Amplify + AppSync 不改变对象
- javascript - 将参数从 HOC 功能组件传递到组件不起作用
- git - git-review 补丁集在推送之前拉取/重新定位最新版本