重新启动后阻止USB_DEVICE_ATTACHED启动应用程序 [英] Prevent USB_DEVICE_ATTACHED launching app after a reboot

查看:731
本文介绍了重新启动后阻止USB_DEVICE_ATTACHED启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动正在监听USB_DEVICE_ATTACHED广播意图,该意图在用户插入我的USB设备并选择我们的应用程序时启动.我的问题是android主机设备每天(通常是凌晨4点左右)重启一次,这时附加的USB_DEVICE_ATTACHED意图也会自行触发.

I have an activity listening for the USB_DEVICE_ATTACHED broadcast intent, which launches when the user plugs in my USB device and selects our app. My problem is the android host devices reboot themselves once a day (usually around 4am), at which point the attached USB_DEVICE_ATTACHED intent is also fired by itself.

在这种情况下,我绝对不想启动我的活动,因为它不知道如何关闭自身.如何防止这种情况发生?我尝试使用PowerManager.isScreenOn()布尔值不启动我的活动(目标是API 17),但它认为屏幕确实处于打开状态.

In this circumstance, I definitely don't want to launch my activity as it doesn't know how to close itself. How can I prevent that from happening? I tried to use the PowerManager.isScreenOn() boolean to not launch my activity (target is API 17), but it thinks the screen is indeed on.

 PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        if (powerManager.isInteractive()) {
            startActivity(intent);
        }
    }
    else{
        //noinspection deprecation
        if (powerManager.isScreenOn()) {
            startActivity(intent);
        }
    }

推荐答案

我认为您没有足够的信息来确定USB_DEVICE_ATTACHED广播是否是由于您的用户插入了某些东西,还是设备自行重启了.

I don't think you have enough information to determine if the USB_DEVICE_ATTACHED broadcast is due to your user plugging something in, or the device rebooting itself.

无论如何,如果用户在一定时间段(1分钟或10分钟或任何时间)内未执行任何操作,我将启动Activity并将其关闭.

What I would do is launch the Activity in any case and have the Activity close itself if the user hasn't performed any action within a certain period of time (1 minute or 10 minutes or whatever).

这篇关于重新启动后阻止USB_DEVICE_ATTACHED启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆