关闭通知栏
当应用获取到DeviceOwner权限后,该应用就可以使用 DevicePolicyManager 类中的 API 来控制设备的一些行为了,具体实现如下:
注册一个自定义广播接收器
package com.android.jarvis.receivers import android.app.admin.DevicePolicyManager import android.content.BroadcastReceiver import android.content.ComponentName import android.content.Context import android.content.Intent import android.os.Build import androidx.annotation.RequiresApi /** * adb shell am broadcast -a com.android.jarvis.statusbar --es setstatus [enable|disable] */ class StatusBarReceiver : BroadcastReceiver() { @RequiresApi(Build.VERSION_CODES.M) override fun onReceive(context: Context, intent: Intent) { val devicePolicyManager = context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager val command = intent.getStringExtra(COMMAND) if (command == COMMAND_ENABLE) { devicePolicyManager.setStatusBarDisabled(getComponentName(context), false) } else if (command == COMMAND_DISABLE) { devicePolicyManager.setStatusBarDisabled(getComponentName(context), true) } } companion object { private const val COMMAND = "setstatus" private const val COMMAND_ENABLE = "enable" private const val COMMAND_DISABLE = "disable" } /** * 获取ComponentName,DevicePolicyManager的大多数方法都会用到 */ private fun getComponentName(context: Context): ComponentName { return ComponentName( context.applicationContext, JarvisAdminReceiver::class.java ) } }
在AndroidManifest.xml中注册广播
<receiver android:name=".receivers.StatusBarReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="com.android.jarvis.statusbar" /> </intent-filter> </receiver>
安装好应用,然后激活DeviceOwner权限,执行下面的命令即可控制通知栏的启用和禁用:
adb shell am broadcast -a com.android.jarvis.statusbar --es setstatus [enable|