I have written code to turn screen on/off based on the brightness. I am able to turn screen off but when screen is turned on the brightness is updated to 1 but screen doesn't wakes up and shows itself. I have to use the manual lock/unlock button on the device.
Is there something missing from my code? I am using Android 2.3.4
if (command.equals("ON")) {
runOnUiThread(new Runnable() {
#Override
public void run() {
onResume();
WindowManager.LayoutParams screenBrightness = getWindow()
.getAttributes();
screenBrightness.screenBrightness = 1;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
getWindow().setAttributes(screenBrightness);
}
});
WMLP = getWindow().getAttributes();
System.out.println("Screen Brightness ON: "
+ WMLP.screenBrightness);
} else if (command.equals("OFF")) {
runOnUiThread(new Runnable() {
#Override
public void run() {
WindowManager.LayoutParams screenBrightness = getWindow()
.getAttributes();
screenBrightness.screenBrightness = 0;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(screenBrightness);
}
});
WMLP = getWindow().getAttributes();
System.out.println("Screen Brightness OFF: "
+ WMLP.screenBrightness);
}
To programmatically turn off the screen and turn on the screen you can use Device Policy Manager.
To turn on the screen you can use PowerManager WakeLock (Wakelock is deprecated but currently working fine).
Use DevicePolicyManager.locknow(); to lock the screen. For that you need to register DevicePolicyManager in you app. A tutorial can be found here.
and use the code below to unlock the device.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
Related
I want to set the wakelock time to the "unlimited" time or at least set the time to xx minutes / hours.
If I try these code :
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
It just give me 30 seconds for the wakelock. But if I change my code to wl.acquire(10*60*1000L /10 minutes/); as suggested from Android Studio, it didn't give any change to the wakelock time, any idea of it ?
Make sure you have the Manifest Permissions tag
<uses-permission android:name="android.permission.WAKE_LOCK" />
make sure to call wakeLock.release() when leaving the activity
#Override
protected void onDestroy() {
wakeLock.release();
super.onDestroy();
}
Wakelock doesn't exactly mean it will keep your screen on.
if you want to keep the screen on :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
this.setTurnScreenOn(true);
} else {
final Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
then when you want to turn it off :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
this.setTurnScreenOn(false);
} else {
final Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
Am developing a voip application. There is a background service which shows incoming call notifications, which works as expected (showing an incoming call dialog) when the phone is not locked and app is in background state.
How can i generate a dialog with interactive buttons, like whatsapp incoming call notification; even when the phone is locked?
Any heads up on this one or documentation that i can look up?
I can send an inapp notification for an incoming call, but that seems to be not enough for the purpose. I would need a full blow dialog interface which has a button or similar that would in turn open the application.
I am using Quickblox as the voip service provider.
Thanks in advance
I have tried unlocking the phone, on a button press
Here is my code to open a dialog from the background service.
viewToShowOnLockedScreen = View.inflate(getApplicationContext(), R.layout.activity_background_call, null);
viewToShowOnLockedScreen.setTag(TAG);
int top = getApplicationContext().getResources().getDisplayMetrics().heightPixels / 2;
final WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, Utils.dpToPx(300), 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON ,
PixelFormat.RGBA_8888);
viewToShowOnLockedScreen.setVisibility(View.VISIBLE);
Animation mAnimation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_slide_in_top);
viewToShowOnLockedScreen.startAnimation(mAnimation);
mWindowManager.addView(viewToShowOnLockedScreen, mLayoutParams);
mWindowManager.updateViewLayout(viewToShowOnLockedScreen, mLayoutParams);
And here is the code to unlock the device on a button press. Though this looks like it unlocks the phone, the screen is on, but the phone is still locked. Home button does'nt work.
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
Not really an answer, a work around i should say.
I coul'dnt unlock the screen. However I have updated the application to listen to Intent.ACTION_USER_PRESENT and added necessary to logic in the app to answer calls once the user has unlocked the device at the point of incoming call. Here is the code.
mUnlockStatusFilter = new IntentFilter();
mUnlockStatusFilter.addAction(Intent.ACTION_USER_PRESENT);
mUnlockStateIntentReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent i) {
if (i.getAction().equals(Intent.ACTION_USER_PRESENT)) {
//Do something phone is unlocked
Log.d(TAG,"Screen unlocked");
if(isWaitingForUnlock){
stopCallNotification();
if(Foreground.get().isForeground()){
Log.d(TAG, "App is in foreground; Sending a broadcast");
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setAction(BaseActivityWithSignalling.onReceiveNewSession);
intent.putExtra("code", BaseActivityWithSignalling.onReceiveNewSessionCode);
sendBroadcast(intent);
Log.d(TAG, "Send broadcast for onReceiveNewSession");
}else{
Log.d(TAG, "App is in background; Showing activity");
Intent showCallingFromBG = new Intent(LMService.this, BackgroundCall.class);
showCallingFromBG.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(showCallingFromBG);
}
}
}
}
};
registerReceiver(mUnlockStateIntentReceiver, mUnlockStatusFilter);
I am trying to show a pop-up on the lock-screen. And also after a interval of time the screen have to go to sleep. It is working with acquiring the wakelock and releasing it after a period of time on a Activity. But the problem is if the screen is touched when showing the popup, then the screen is not sleeping. Here is the code that i have used.
mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
wl = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
wl.acquire();
And for releasing wakelock after a period .
new Handler().postDelayed(touchTimer, 1000 * 12);
Runnable touchTimer = new Runnable() {
#Override
public void run() {
if(wl.isHeld())
wl.release();
}
};
I've been looking around for the last days and none of the answers to the questions made have helped me and I'm bumping my head with something that perhaps is simple to solve .... I want to work with the proximity sensor and what I'm doing is that if I put the finger on the sensor it turns off the screen and if I remove the finger away it turns back on the screen! I'm sucessfully turning off the screen and I'm getting the "Log.i("info", "trying to turn on!")" message when I remove the finger but somehow the screen doesn't turn on .... I've tried with wakelock (commented) and with flags with no sucess! If I remove the finger the lights on the keypad turn on but the screen won't .. If I press the power button two times it turns on the screen sucessfully! Can anyone give me an help with it? :(
#Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
ProximityReading.setText("\nProximity Sensor Reading:" + String.valueOf(event.values[0]));
}
if(event.values[0] == 0) {
WindowManager.LayoutParams params = getWindow().getAttributes();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
params.screenBrightness = 0f;
getWindow().setAttributes(params);
} else {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1f;
getWindow().setAttributes(params);
/*powermanager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powermanager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wakeLock.acquire();
Log.i("info", "trying to turn on!");
}
}
if you want turn screen on,you can use the API such as "TurnScreenOn" in PowerManager,or use ACQUIRE_CAUSES_WAKEUP combined with FULL_WAKE_LOCK
I use the following method to unlock the phone,lighten it up and then reenable the keyguard lock.
public void unlockAndPowerup(){
KeyguardManager km = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
wakeLock.release();
kl.reenableKeyguard();
}
Your procedure is wrong. It may work on some devices by chance, but that's not the way to do it.
FLAG_TURN_SCREEN_ON is supposed to be applied on the onCreate method of an activity. You need to modify your app logic.
Your screen off technique is incorrect. You're modifying brightness, that's a hacky way, and may not turn off screen at all, and drain battery.
You need to achieve it via Device Admin permission. That's the official way to do it
Locking sample code
public static void lockNow(Context c) {
DevicePolicyManager dpm = (DevicePolicyManager)c.getSystemService(Context.DEVICE_POLICY_SERVICE);
if(dpm.isAdminActive(new ComponentName(c.getApplicationContext(),DeviceAdmin.class)))
dpm.lockNow();
}
DeviceAdmin.java
import android.app.admin.DeviceAdminReceiver;
public class DeviceAdmin extends DeviceAdminReceiver {
}
In the manifest
<receiver
android:name=".admin.DeviceAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
And asking for Device Admin permission
private void askAdminPerm(){
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
new ComponentName(root.getContext(), DeviceAdmin.class));
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, getString(R.string.lock_msg_big));
startActivity(intent);
}
I can't turn off the screen using this code. I used PowerManager and wl.release() method, but it doesn't work. Can somebody show me an example?
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
This is part of my function:
stateString = "nextone";
if(stateString=="nextone"){
wl.release();
}
I also added permission in the manifest but no result.
I found the answer over here on stack overflow: Turn off screen on Android
Copied from there:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
I tried this out and it seems to work.
If you don't use a permission, the program will crash with a SecurityException when it tries to lock, so that isn't the problem. The correct method is: (obtains WakeLock on start, gives it up when the application loses focus (onPause)
//declared globally so both functions can access this
public PowerManager.WakeLock wl;
///////////onCreate
//stop phone from sleeping
PowerManager powman = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = powman.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "NameOfLock");
wl.acquire();
///////////onPause
wl.release();
//////////for completion's sake, onResume
if(!wl.isHeld()){
wl.acquire();
}
However, your problem is actually in this check
if(stateString=="nextone")
This should be if(stateString.equals("nextone"))
please check this link before proceeding with wake lock. if it does not solve your problem then you can proceed with wake lock.
Force Screen On
You can use
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
try
{
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000*15);
}
catch (NumberFormatException e)
{
Log.e("aa", "could not persist screen timeout setting", e);
}
How to detect switching between user and device