代码取消device-owner

  public void removeDeviceOwner(final Context context) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (isDeviceOwnerApp(context)) {
                    NsLog.d(TAG, "========removeDeviceOwner=========");
                    DeviceManagerReceiver.handleDeviceAdminDisable(context);
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            DevicePolicyManager mDevicePolicyManager = getDPM(context);
                            mDevicePolicyManager.clearDeviceOwnerApp(context.getPackageName());
                        }
                    },1000*6);
                }
            }
        } catch (Exception ex) {
            NsLog.e(TAG, "exception while removeDeviceOwner:" + Log.getStackTraceString(ex));
        }
    }


真正有效果的是run方法中的代码

 public void removeDeviceOwner(final Context context) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (isDeviceOwnerApp(context)) {
                    Log.d(TAG, "========removeDeviceOwner=========");
                 //   DeviceAdminReceiver.handleDeviceAdminDisable(context);
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            DevicePolicyManager mDevicePolicyManager = getDPM(context);
                            mDevicePolicyManager.clearDeviceOwnerApp(context.getPackageName());
                        }
                    }, 1000 * 6);
                }
            }
        } catch (Exception ex) {
            Log.e(TAG, "exception while removeDeviceOwner:" + Log.getStackTraceString(ex));
        }
    }

    @TargetApi(18)
    public boolean isDeviceOwnerApp(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            DevicePolicyManager manager = getDPM(context);
            if (manager.isDeviceOwnerApp(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }

    private DevicePolicyManager getDPM(Context context) {
        return (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Linux亮灯驱动代码,可用于控制板载LED灯。 ```c #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/device.h> #include <linux/cdev.h> #include <asm/uaccess.h> #include <asm/io.h> #define DEVICE_NAME "led" // 设备名 #define LED_ON 1 // LED亮 #define LED_OFF 0 // LED灭 static int major; // 主设备号 static struct class *led_class; // 设备类 static struct device *led_device; // 设备 // 硬件地址映射 static void __iomem *gpio_base = NULL; // LED灯控制函数 static void led_control(int status) { u32 val; val = readl(gpio_base); if (status == LED_ON) { val &= ~(1 << 1); // LED亮 } else { val |= 1 << 1; // LED灭 } writel(val, gpio_base); // 写入寄存器 } // 打开设备 static int led_open(struct inode *inode, struct file *file) { return 0; } // 关闭设备 static int led_release(struct inode *inode, struct file *file) { return 0; } // 读取设备 static ssize_t led_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { return 0; } // 写入设备 static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { char data; int ret; ret = copy_from_user(&data, buf, sizeof(data)); // 从用户空间获取数据 if (ret) { printk(KERN_ALERT "copy_from_user failed!\n"); return -EFAULT; } led_control(data); // 控制LED灯 return sizeof(data); } // 设备操作函数 static struct file_operations led_fops = { .owner = THIS_MODULE, .open = led_open, .release = led_release, .read = led_read, .write = led_write, }; // 模块加载函数 static int __init led_init(void) { int ret; printk(KERN_ALERT "led driver init!\n"); // 映射GPIO寄存器 gpio_base = ioremap(0x11000000, 0x100); // 分配主设备号 ret = alloc_chrdev_region(&major, 0, 1, DEVICE_NAME); if (ret < 0) { printk(KERN_ALERT "alloc_chrdev_region failed!\n"); return ret; } // 创建设备类 led_class = class_create(THIS_MODULE, DEVICE_NAME); if (IS_ERR(led_class)) { unregister_chrdev_region(major, 1); printk(KERN_ALERT "class_create failed!\n"); return PTR_ERR(led_class); } // 创建设备 led_device = device_create(led_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME); if (IS_ERR(led_device)) { class_destroy(led_class); unregister_chrdev_region(major, 1); printk(KERN_ALERT "device_create failed!\n"); return PTR_ERR(led_device); } // 注册设备驱动 cdev_init(&led_cdev, &led_fops); led_cdev.owner = THIS_MODULE; ret = cdev_add(&led_cdev, MKDEV(major, 0), 1); if (ret < 0) { device_destroy(led_class, MKDEV(major, 0)); class_destroy(led_class); unregister_chrdev_region(major, 1); printk(KERN_ALERT "cdev_add failed!\n"); return ret; } return 0; } // 模块卸载函数 static void __exit led_exit(void) { printk(KERN_ALERT "led driver exit!\n"); // 注销设备驱动 cdev_del(&led_cdev); device_destroy(led_class, MKDEV(major, 0)); class_destroy(led_class); unregister_chrdev_region(major, 1); // 取消硬件地址映射 iounmap(gpio_base); } module_init(led_init); module_exit(led_exit); MODULE_LICENSE("GPL"); ``` 这段代码包括了设备的创建、驱动的注册、硬件地址的映射、LED灯的控制等功能,可以根据实际需要进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值