Prevent USB_DEVICE_ATTACHED launching app after a reboot

谁说我不能喝 提交于 2019-12-11 07:16:06

问题


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.

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

回答1:


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.

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).



来源:https://stackoverflow.com/questions/45814702/prevent-usb-device-attached-launching-app-after-a-reboot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!