Using Power Manager - java

My app sets an alarm. While waiting for the alarm my phone closes the screen. When the alarm sounds the screen is dark so I press the power key and the screen lights up but I now have to touch the Lock button to access the app interface. I would prefer that the Lock has been disposed off when the user responds to the alarm so I tried to use the power manager. The alarm receiver starts a new activity so I initialised the power manager in the onCreate for this activity. However this causes a force close error. I call the power manager as follows
PowerManager pm = (PowerManager)cText.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK , TAG);
wl.acquire();
Any suggestions please.

Did add the uses permisson for power mananger in your manifest?

Since the person asking the question states that he "has to touch the lock" button I assume that he is speaking about the Key Guard. (Pattern, Pin etc). You cannot use the PowerManager API to disable this. Instead you should diable the KeyGuard
KeyguardManager keyguardManager =
(KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock =
keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
To My knowledge, this is the correct way to disable the Screen Lock.
You also need this Permission
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

You can take wl in Application context so once its initlize and set back to that and after that when you release that you will set null and check that it again if its null than init again other wise take it as it is.

Related

How force the app to opt out of battery saver mode when the service is ON?

The expected behavior is that the app will be running all the time when it's in ON state. Some phones put the app in background mode when the app is not active for some time. I want the app to be running all the time even its in standby mode(standby mode means when we press the home button the app will go to background. and it will run for some time).
I found following code and I tried that
PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE);
String packageName = "org.traccar.client";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent i = new Intent();
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
i.setData(Uri.parse("package:" + packageName));
startActivity(i);
}
else{
i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
i.setData(Uri.parse("package:" + packageName));
startActivity(i);
}
}
Even after working with the code the default state is Battery Saver(recommended)
I want the app in No Restriction mode once the app is opened, any solution for this?
The code you use is for battery optimization. Settings-->Batery-->Three Dots Menu Item (...)--->Battery Optimization-->(Choose an app from list)--->Optimize/ Don't optimize.
By choosing Don't optimize you are essentially bypassing Doze, not app standby.
Also be advised that doing this programmatically as you do may result in Google taking your app off the store. It is safer to do it manually following the path i described above.
More on Doze and App Standby here

How to sleep my android device at an specific time and wake up at some another time programmatically?

I am creating an android app which has functionality to sleep the device based on certain conditions and wake up based on some other conditions. Before API 21, there was a method powerManager.goToSleep() in PowerManager which used to do the trick. But, now the same method is not accessible anymore. Is there any other way to do that?
int defaultTurnOffTime = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 60000);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, defaultTurnOffTime);
Above mentioned code is something I have got so far. But, it doesn't seem to work for me. Please suggest if there is any way to achieve the functionality.
Have you tried using PowerManager.Wakelock ?
Acquires the wake lock with a timeout.
Ensures that the device is on at the level requested when the wake lock was created. The lock will be released after the given timeout expires.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
..screen will stay on during this section..
wl.release();
there are 4 types of Flags
Flag Value CPU Screen Keyboard
PARTIAL_WAKE_LOCK On* Off Off
SCREEN_DIM_WAKE_LOCK On Dim Off
SCREEN_BRIGHT_WAKE_LOCK On Bright Off
FULL_WAKE_LOCK On Bright Bright

Turn screen off programmatically

I'm working in an app that needs to to do any of this actions:
Lock the device
Put the device in sleep mode
Turn off the screen
How can I achieve this?
Found this option, but it requires the proximity sensor to be in "near" state to turn off the screen:
mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "tag");
mWakeLock.acquire();
Thanks
If your app is a device admin you can lock the screen with DevicePolicyManager.lockNow()
You can use this snippet (after setting the Device Admin part):
DevicePolicyManager manager = ((DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE));
manager.lockNow();

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?

How to handle back pressed if android kill process

I googled but not find helpful information about this. If I open app and go through these activities A-B-C-D, app has 4 activity in back stack. Android can kill process, what he actually does, after restoring app I see activity D, so how I can handle this behavior, how to check how many real activity I have in back stack.
I tried:
ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningTaskInfo> taskList = mngr.getRunningTasks(10);
Log.d("myTag",taskList.get(taskList.size()-1).numActivities+"");
Or
ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningTaskInfo> taskList = mngr.getRunningTasks(10);
if(taskList.get(0).numActivities == 1 && taskList.get(0).topActivity.getClassName().equals(this.getClass().getName())) {
Log.i(TAG, "This is last activity in the stack");
}
But it show the same number before and after killing process.
I want to redirect user to actviity A if my app was terminated, and back pressed event will close app.
You should handle with this using onSavedInstanceState.
Check this image:
The guideline says:
The two ways in which an activity returns to user focus with its state intact: either the activity is destroyed, then recreated and the activity must restore the previously saved state, or the activity is stopped, then resumed and the activity state remains intact.
So, you should save the instance state and restore this at onRestoreInstanceState then you can do what you want based on previous state of your activity.
Check this link for reference: http://developer.android.com/guide/components/activities.html#ConfigurationChanges
Hope that it helps.

Categories