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