I hope this question wasn´t asked before, but I couldn´t find any solution for my problem. I have a widget which is also used for keyguard. I want to add an onClick handler for my widget. There is no problem, when the widget is on home-screen, the activity starts normally. But on keyguard the activity is launched, but isn´t shown, because the device is locked.
I don´t want to dismiss the keyguard with flags like WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED and WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD. I just want a little window to be shown, where I am asked to unlock the device, like it is opened when you click on several keyguard widgets, like for the android e-mail application.
Is there another flag to achieve this? Or do I have to do something with my intent, called to open the activity? Thanks for help.
For enabling or disabling lock screen in Android, we need to get the instance of keyBoard Manager, using getSystemService(). The syntax is as,
KeyguardManager keyguardManager =
(KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
For locking the screen use,
lock.reenableKeyguard();
and for disabling the lock use,
lock.disableKeyguard();
This will just hide the lock screen and will display if any incoming call or any such event is happening, it will display that screen. It wont display the lock screen.
When running the application, to disable from going to the lock state, just use
setKeepScreenOn() is set to true. or use the xml attribute android:keepScreenOn="true"
Another way to prevent the device to go to the sleep or lock mode when the application is running is set this api to true - setKeepScreenOn()
And of course we need to give permission android.permission.DISABLE_KEYGUARD
Related
For my android app, I want to keep the screen from dimming while certain activities (e.g. CatActivity) are in the resume state. Does anyone know how to do that? is there a way to declare so in the manifest or in the activity itself?
UPDATE
I found the answer:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Is there a way to turn it off? I don't see a removeFlags method.
To delete the flag simply call the clearFlags() method:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
i built an activity that can launch itself when i hit a certain button-combination on my locked screen. I also managed to keep the activity running despite pressing the powerButton two times --> screen off and screen on --> activity is still running and keyguard still retired from duty. This is done by adding flags in the onCreate()-method of my activity.
i.e. getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Now i want to achieve the same with the Intent.ACTION_CALL. But the problem is that i can't override any method of that intent as it seems to be exclusively run by android. Also, adding flags to the intent before i hit startActivity doesn't work as i can't use the necessary flags here.
Does anyone have an idea how i can achieve it?
If I understood correct, you want to make another activity, you starting with Intent.ACTION_CALL action, to add some flags to it's window.
It's not possible on Android. It means another app should give everyone rights to control itself in order to do this.
You can only add flags to Intent for controlling new activity start and tasking options.
See official documentation, you can use flags, that starts with FLAG_ACTIVITY
I wrote a simple alarm-clock style application that I run on my (jailbroken) Nook Simple Touch (aka NST), under Android 2.1.
When the scheduled alarm time arrives, my application needs to wake up the NST and display a page of HTML content. I use AlarmManager to get a callback at the right time, and it seems to work as expected -- almost.
The problem occurs when enough idle time has passed that the NST has activated its lock-screen mode (i.e. it is auto-displaying a caricature of a famous author). I can't find a programmatic way to dismiss the lock-screen so that my HTML content will be visible. I can see that my alarm callback routine ran at the expected time (via the LogCat view in Eclipse, after I reconnect to the NST with adb), and after I manually "drag to unlock" with my finger, I can see that my app's window updated as expected, but I need to have the text become visible when the alarm event occurs, not just after the user unlocks the device. I tried the code shown below (based on other StackOverflow answers) but it doesn't help.
Any ideas regarding a way to do this? (One solution that technically works is to keep FLAG_KEEP_SCREEN_ON set on my window at all times, so that the famous-author-lock-screen never appears in the first place, but that keeps the NST awake and therefore it uses up the battery rather quickly, so I want to avoid that if possible)
private void wakeUpTheScreen()
{
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);
}
Ha, I figured out (with some more help from previous StackOverflow answers) what I was doing wrong.
The problem is as described in the above link -- the AlarmManager was calling my BroadcastReceiver as expected, and then my BroadcastReceiver would sendMessage() a Message to my AlarmHandler (as shown in the Alarm example I was cribbing from). But the Nook would go back to sleep immediately after onReceive() returned, which meant that the secondary handler never got called, and therefore my wakeUpTheScreen() method wasn't getting executed.
I moved the wakeUpTheScreen() call so that it now gets called directly from the onReceived() method, and now the wakeup works as expected. :^)
in my case, I just start other activity to remove lock screen.
However, I saw any app to dismiss lock screen and that is really smooth.
I think the way is different with me..
plz~ tell me how to dismiss lock screen~
and how do i use keyguard to do that?
in advance, thank you!!
If you're talking about the keyguard, then you want to take a look at the KeyguardManager class. You can get an instance of it like this:
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
Check out this question for some additional code on how to disable it.
If you're talking about the lock screen, where the user has to enter a password -- I don't think it's possible to bypass this.
I have tried to find a way to disable the PatternLock screen temporarily. I don't want the lock to be disabled completely, but the user should not need to re-enter his pattern all the time.
My idea is to write a service which disables the pattern after some user activity and re-enables it after a while. (and even more)
There are apps on the market that do something like that (i.e. AutoLock or TogglePattern), so there must be a solution.
I know that I can prevent a lock completely by using:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
or
KeyguardLock.disableKeyguard()
But that is not what I'm after.
I saw the class com.android.internal.widget.LockPatternUtils in the android sources which is used by the settings activity, but this class is not (at least as far as I know) accessible by a "normal" application.
Do you have any suggestions?
As of 2.0 (API level 5), you can use this window flag to prevent the lock screen from being displayed while your window is shown:
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED
You can also use this flag to allow a non-secure keyguard to be dismissed when your window is displayed:
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD
Note that these do not allow you to bypass the lock screen outside of your application's environment, which is an intentional design decision.
There is also an older API that lets you hide the lock screen in a similar way to a wake lock:
http://developer.android.com/reference/android/app/KeyguardManager.html#newKeyguardLock(java.lang.String)
Use of this API is discouraged on newer platforms, because it is very easy to get wrong and cause bad behavior (the screen not locking when the user would expect it to), and basically impossible to have clean transitions between activities with unlocked states. For example, this is the API that the in-call screen originally used to hide the lock screen when it was displayed, but as of 2.0 it has switched to the new cleaner window flags. Likewise for the alarm clock etc.
Have you tried looking at the code for com.android.internal.widget.LockPatternUtils and doing what it does?
It has something like this:
public void setLockPatternEnabled(boolean enabled) {
setBoolean(android.provider.Settings.System.LOCK_PATTERN_ENABLED, enabled);
}
private void setBoolean(String systemSettingKey, boolean enabled) {
android.provider.Settings.System.putInt(
mContentResolver,
systemSettingKey,
enabled ? 1 : 0);
}
You might be able to do something similar in your code.