public void screenTurnALWAYSON() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
I have this code, this keep the screen always on. How can I reset these settings when I don't need it anymore?
With the clearFlags method, f.e.:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Use WakeLocker.acquire(context); and WakeLocker.release();.
you'll need <uses-permission android:name="android.permission.WAKE_LOCK" /> permission.
Related
I have an application that opens an Activity on a certain event.
Just like alarm application. I use the following flags:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
);
The application creates a wake lock for that purpose.
On most devices it works fine, but on Xiaomi Redme 2(Android 6) it does not.
When the application launches the Activity, the screen is turned on and the lock screen displayed. My Activity is NOT displayed.
After I enter password I see my Activity.
Then I have changed the package name, and Activity displayed successfully
without a lock screen.
It looks as though Xiaomi has black listed our original app for some reason.
I wonder if anyone has encountered this behavior and has a solution?
This is a Xiaomi/MIUI specific issue.
You need to grant special permission to an app to unlock the screen during alarm.
Go to System Settings > Permissions > Advanced Permissions > select the app and give it permission to access Lockscreen.
Source
https://sleep.urbandroid.org/faqs/?Display_FAQ=22281
FLAG_SHOW_WHEN_LOCKED is deprecated. You can also use this in old api,
final Window win= getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
And new usage, try this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keyguardManager.requestDismissKeyguard(this, null);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1){
setShowWhenLocked(true);
setTurnScreenOn(true);
}
I want to make something like alarm activity, but it only triggered over certain push notification.
I've been pulling my hair to achieve that, combining answer here and there(and many more that i couldn't refer here). Now I already can show the activity over the lock screen, but i'm stuck on getting the onClick event listener to dismiss the alarm.
So far here's my codes:
it's onCreate of my Activity:
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
setContentView(R.layout.alarm);
ButterKnife.bind(this);
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Alarm");
wakeLock.acquire();
dismissButton.setOnClickListener(v -> Log.w("button", "Clicked"));
the Activity on manifest:
<activity android:name=".{package}.Alarm"
android:showOnLockScreen="true"
android:theme="#style/AppTheme.WhiteAccent"
android:screenOrientation="sensorPortrait"/>
and it's where i call my Activity from my notificationService
Intent intent = new Intent(this, MewsAlarm.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("judul", data.get("title"));
startActivity(intent);
am I did it wrong?
I only test it on my phone though (4.4.2), so i don't even know the activity will shown on other device. But really, can someone help me to get over this please?
thank you for the suggestion guys, my bad i haven't search the related topic thoroughly, so i found the answer over here:
https://stackoverflow.com/a/4482868/7852331
so i just add
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
to my flags above, and voila i get the click event.
I have implemented a caller ID feature where when the screen is locked it should open the lock and show the popup with the name of the person calling.
this is the code i've added in the onCreate() of the PopupActivity.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
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);
I've also added the permissions
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
This works fine when the phone is not password/pattern protected.However it shows a black background to the popup window when the device is password/pattern protected disabling the user from attending/rejecting the call.
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 |PixelFormat.TRANSLUCENT);
This seems to be working for me
I want to wake up and unlock a device. Then, I'd like show an activity when the user has new message from Firebase.
I wrote this on onResume() method:
window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
This works for devices with API > 19. The problem is that in KitKat, it either does nothing or it wakes up the screen but doesn't unlock the device.
Also I set the right permissions in the AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Also in MyFirebaseMessagingService.class, I'm starting an activity with FLAG_ACTIVITY_NEW_TASK as flag.
Does anyone know what am I missing?
Thanks for the help.
Ok i found it. I just add that piece of code and it worked
KeyguardManager manager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock = manager.newKeyguardLock("abc");
lock.disableKeyguard();
I'm making my alarm app. There is an Activity to show alarm infomation.
I want turn on the screen and unlock it. I wrote these code
AlarmActivity.java:
public class AlarmActivity extends Activity {
......
void onCreate(Bundle bl) {
.....
final Window win = getWindow();
win.requestFeature(android.view.Window.FEATURE_NO_TITLE);
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
LayoutInflater inflater = LayoutInflater.from(this);
setContentView(inflater.inflate(R.layout.alarm, null));
}
......
}
AndroidManifest.xml
{activity android:name="AlarmTaskActivity"
android:excludeFromRecents="true"
android:theme="#android:style/Theme.Wallpaper.NoTitleBar"
android:launchMode="singleInstance"
android:taskAffinity=""
android:configChanges="orientation|keyboardHidden|keyboard|navigation"/}
It's ok,but when I change
android:theme="#android:style/Theme.Wallpaper.NoTitleBar"
to
android:theme="#android:style/Theme.Dialog"
the screen did not turn on nor unlock, I am really confused....
Can you please tell me how to make the screen turn on and unlock when I use "#android:style/Theme.Dialog"?
Thanks
by the way,I have android 2.0 in my test device.
In order to activate the screen and force it to stay on, you need to use a WakeLock. See the documentation for more information. Changing the theme along won't cause the screen to wake up. You'll need to use the ACQUIRE_CAUSES_WAKEUP flag in order to do that.