代码取消device-owner

本文详细介绍了如何在Android设备上移除应用的设备所有者权限。通过使用DevicePolicyManager API,可以在满足特定条件的情况下,延迟执行清除设备所有者应用的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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);
    }

### STM32下的SPI协议代码实现 以下是基于STM32微控制器的SPI协议代码实现示例。该代码展示了如何配置和初始化SPI外设以及通过它传输数据。 #### 配置GPIO引脚 在使用SPI之前,需要先配置相关的GPIO引脚作为SPI功能引脚: ```c void GPIO_SPI_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 启用GPIOA时钟 GPIO_InitTypeDef GPIO_InitStructure; // PA5 (SCK), PA6 (MISO), PA7 (MOSI) 设置为复用推挽输出模式 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 设置速度为50MHz GPIO_Init(GPIOA, &GPIO_InitStructure); } ``` #### 初始化SPI外设 接下来是对SPI外设本身的初始化过程: ```c #include "stm32f10x.h" void SPI_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); // 启用SPI1时钟 SPI_I2S_DeInit(SPI1); // 取消初始化SPI1寄存器到默认状态 SPI_InitTypeDef SPI_InitStruct; SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // 全双工模式 SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // 主机模式 SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // 数据帧大小为8位 SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // 时钟极性低电平有效 SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // 第一跳变沿采样 SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; // 软件NSS管理 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; // 波特率分频因子=2 SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; // MSB优先发送 SPI_Init(SPI1, &SPI_InitStruct); SPI_Cmd(SPI1, ENABLE); // 开启SPI1 } // 发送单字节数据并通过SPI接收返回的数据 uint8_t SPI_SendReceiveByte(uint8_t byte_to_send) { while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); // 等待发送缓冲区为空 SPI_SendData8(SPI1, byte_to_send); // 发送一个字节 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); // 等待接收到数据 return SPI_ReceiveData8(SPI1); // 返回接收到的数据 } ``` 上述代码实现了基本的SPI主机端通信逻辑[^3]。 --- ### Linux环境下的SPI驱动程序示例 对于Linux环境下开发SPI驱动的情况,可以参考如下简单的SPI驱动框架代码: ```c #include <linux/spi/spi.h> #include <linux/module.h> static int spi_example_probe(struct spi_device *spi_dev) { dev_info(&spi_dev->dev, "Device probed successfully\n"); return 0; } static int spi_example_remove(struct spi_device *spi_dev) { dev_info(&spi_dev->dev, "Device removed\n"); return 0; } static struct spi_driver example_spi_driver = { .driver = { .name = "example-spi", .owner = THIS_MODULE, }, .probe = spi_example_probe, .remove = spi_example_remove, }; module_spi_driver(example_spi_driver); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Example Author"); MODULE_DESCRIPTION("Simple SPI Driver Example"); ``` 此代码片段展示了一个基础的Linux SPI驱动注册流程[^2]。 --- ### 用户空间SPI访问示例 如果希望从用户空间直接操作SPI设备,则可以通过`/dev/spidevX.Y`接口完成。下面是一个简单例子: ```c #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <sys/ioctl.h> #include <linux/spi/spidev.h> #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) int main() { uint8_t tx[] = {0xFF, 0xA5}; uint8_t rx[ARRAY_SIZE(tx)] = {0}; int fd; if ((fd = open("/dev/spidev0.0", O_RDWR)) < 0) { perror("Failed to open the device..."); return -1; } struct spi_ioc_transfer tr = { .tx_buf = (__u64)tx, .rx_buf = (__u64)rx, .len = ARRAY_SIZE(tx), .speed_hz = 500000, .delay_usecs = 0, .bits_per_word = 8, }; ioctl(fd, SPI_IOC_MESSAGE(1), &tr); printf("Received data: %02X %02X\n", rx[0], rx[1]); close(fd); return 0; } ``` 这段代码说明了如何利用`ioctl()`函数调用来与指定的SPI设备交互[^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值