Turning Sleeping Android Device on in BroadcastReceiver Class - java

I have a broadcastreceiver class that starts a service. What I want to happen is when it starts the service it should also turn the device screen on. I haven't found a solution to implement. I tried extending the WakefulBroadcastReceiver but it won't open the screen, rather it keeps the device on if it was already on. Does anyone know of a solution?

try this method
public void turnScreenOn(Context context) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
#SuppressWarnings("deprecation")
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
wakeLock.release();
}
requires the permission
<uses-permission android:name="android.permission.WAKE_LOCK"/>

Related

Programmatically disable the screen on/off magnetic sensor on Android

Several Android devices famously the Nexus series have a magnetic sensor that is NOT the android.hardware.Sensor.TYPE_MAGNETIC_FIELD used exclusively to turn the screen ON/OFF automatically using phone cases that have a small magnet inside them.
On my application, I'm using magnet detection using android.hardware.Sensor.TYPE_MAGNETIC_FIELD and the SensorManager to detect user touches the phone in a case with some magnets inside.
The code works without issues, the problem is that it's extremely easy on those devices to accidentally trigger the screen ON/OFF sensor thou, turning off the screen.
I've tried:
android:keepScreenOn="true" on the XML
using the permission android.permission.WAKE_LOCK and acquiring a wake-lock via PowerManager.
Both without success.
Is there a way to temporarily disable this sensor while my activity is resumed?
The keepScreenOn = true in manifest also doesn't work here as the action of hall effect sensor is same as pressing power button.
Here, I offer a work-around that I have tested. In this approach you display your activity regardless of hall-effect sensor locking the device and turning of the display.
Before beginning the unlocking action
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
credits for above code - https://stackoverflow.com/a/45951927/9640177
Also make sure to add Permissions
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
Setting these flags will make sure that your window will be visible above lockscreen.
Now, listen for ACTION_SCREEN_OFF intent. To do this add a broadcast receiver that listens for this intent broadcasted by the system. You need to register the receiver locally. (stackoverflow.com/a/9478013/9640177)
in manifest
<receiver android:name=".receiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
</intent-filter>
</receiver>
Receiver class
public class receiver extends BroadcastReceiver {
MyActivity activity;
receiver(MyActivity activity){
this.activity = activity;
}
#Override
public void onReceive(final Context context, Intent intent) {
activity.turnOnScreen();
}
}
Inside activity
// register receiver
...
IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
receiver r = new receiver(MyActivity.this);
registerReceiver(r, screenStateFilter);
...
// function to turn on display
public void turnOnScreen(){
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "my:Tag");
wakeLock.acquire();
wakeLock.release();
}
credit - https://stackoverflow.com/a/44706632/9640177
This will turn the display on.
Don't forget to remove these flags after your activity has done the job and does not require to be in forefront.
getWindow().removeFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
This will make your window disappear from lock-screen and make the application function normally.
Hope this helps.
the most pragmatic solution to the problem might be ...
to disable the smart cover feature physically rather than by software.
eg. take a razorblade and tweezers and then remove the magnet.
... or simply get another cover without a magnet.
alternatively, file a bug against drivers/input/lid.c.

Wake up screen and show activity from service

I have an IntentService running background and listening incoming TCP messages. When a desired message has been received, the device should wake up and MainActivity be on top.
I can start a new MainActivity and wake up the device, but then I have many instances of it. The best solution would be sending a broadcast message to the MainActivity, but then I cannot wake up the screen.
I have these permissions set:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
I have this in my onResume()
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
I can call even my onResume() function, but the screen is black. When I set MainActivity as singleTop, starting new MainActivity keeps the screen black.
Any idea?

Skip lock screen automatically after pressing the power button when app is in the foreground

I have been trying for a while to achieve this with no luck.
My app is a GPS tracker for hikers. Users need to look at the map very often, so I want them to be able to press the power button to turn off the screen and then to be able to bring it back when they press it again so they can save battery.
I know it has to be possible, because applications such us Google Maps and Activity Zone skip the lock screen when you do that.
I found these two solutions online but they didn't work for me. The lock mode selected (pattern, PIN, fingerprint) always shows up. I also read by some people that they are deprecated.
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
OR
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "INFO");
wl.acquire();
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
kl = km.newKeyguardLock("name");
kl.disableKeyguard();
Anybody knows how I can do this?

change the status of the power saver (on/off)

i have tried to change the status of the power saver mode from my app i have figured out how to read the status (Through the Power Manager) but how can i change it
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
I Guess There is no API in android SDK to support this, Because power saver might be a third party function given by the device manufactures company and they have there own version of "Power Savers" . You can just use the PowerManager to acquire and release wakelocks.
Use wl.acquire(); and make sure you have this permission added to your manifest,
<uses-permission android:name="android.permission.WAKE_LOCK" />

Attempting to put Android device to sleep, but the PowerManager does not contain a "goToSleep(long) method

I am attempting to put a device to sleep and I have found references all over about using the PowerManager class's goToSleep(long) method but I do not see it in the documentation and it does not work when I attempt to using it in my code.
Android's documentation does not contain a goToSleep method that I could see.
My Code:
private void sleepDevice() {
try {
PowerManager powerMgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
long time = 1000;
powerMgr.goToSleep(time);
} catch (Exception ex) {
updateStatus("Error attempting to reboot device.");
updateStatus(ex.getLocalizedMessage());
}
}
Android Studio does not let the code compile with the message, "Cannot resolve method 'goToSleep(long).
I don't even see this method as deprecated. Also, I don't need to worry about security permissions, the call is intended to run on rooted devices or fail elegantly on non-rooted devices.
You can use the DeviceAdministrator , but you need the user to grant you those rights.
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = manager
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR_OWN_TAG");
wl.acquire();
wl.release();
Try this way and give some feedback.

Categories