Reputation: 131
If my app is running and I press lock screen button, it will put the app in background.What is the method to check whether onPause() is called by screen lock?.Thanks in advance.
Upvotes: 1
Views: 4440
Reputation: 244
You Can Simply Know It By Using This Method
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
System.out.println("On Pause called");
}
For Keeping The Device Awake while lock screen. Documentation.
Ok in your case you would need Wake_Lock
To use a wake lock, the first step is to add the WAKE_LOCK permission to your application's manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
If your app includes a broadcast receiver that uses a service to do some work, you can manage your wake lock through a WakefulBroadcastReceiver, as described in Using a WakefulBroadcastReceiver. This is the preferred approach. If your app doesn't follow that pattern, here is how you set a wake lock directly:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); Wakelock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag"); wakeLock.acquire();
To release the wake lock, call wakelock.release(). This releases your claim to the CPU. It's important to release a wake lock as soon as your app is finished using it to avoid draining the battery.
DO this after setting powermanager.
boolean screenOn;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
screenOn = powerManager.isInteractive();
} else {
screenOn = powerManager.isScreenOn();
}
if (screenOn) {
// Screen is still on, so do your thing here
}
Upvotes: 1
Reputation: 1474
All you have to do is check if the screen is on or not.
@Override
protected void onPause() {
super.onPause();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean screenOn;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
screenOn = pm.isInteractive();
} else {
screenOn = pm.isScreenOn();
}
if (screenOn) {
// Screen is still on, so do your thing here
}
}
Upvotes: 9
Reputation: 2174
You just want to know when onPause is called? You could override the super function and add logging to the function:
@Override
public void onPause() {
super.onPause();
System.out.println("On Pause called");
}
Upvotes: 0