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.
Related
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
I need to register a broadcast receiver that will tell me any kind of Drag events throughout the system. My app will run at background and perform any task if any kind of Drag event happens even any other app is running in the foreground. Is it possible? Any idea on how can I do it?
Updates: Do not think I'm going to make keylogger. My app will be visible but will run in background. And all I want is simply to detect Drag events (drag to left, drag to right, drag to up and drag to down).
I'll accept any answer if you can tell me about how can I display 4 buttons those are permant, on top of any other apps because this can also serve me what I want.
Your app can run without a "normal" UI by running as a Service, as per your link, but I think the code you linked may be slightly out of date.
Remember that your service must run in the foreground - the code is supplied there in your link, but not explicitly called. Without running in the foreground, the system could well stop your app rather than running it in the background.
When I created a task switcher using such overlays, I found it was necessary to use a TYPE_SYSTEM_ALERT rather than TYPE_SYSTEM_OVERLAY.
Android 4.x - System Overlay - Cannot capture touch events
I declare my window parameters without FLAG_WATCH_OUTSIDE_TOUCH.
WindowManager.LayoutParams params =
new WindowManager.LayoutParams(width, height, x, y,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
Your service should also be sure to properly unregister the overlay view from the WindowManager when it ends. Without this, you app will leak memory.
public void onDestroy()
{
super.onDestroy();
if (overlay != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(overlay);
overlay = null;
}
}
I see that this is done in OverlayView.destory() (note the incorrect spelling of that method name - it would be a good idea to use the correct name for that method).
So your real requirement is to be able to pass some input to your app whilst allowing the screen to be dedicated to outputting video.
Have you thought about the following:
detecting tilt or orientation of the device to control direction
having 4 nfc tags, and detecting which of those you are over to change direction (may not give you a quick enough response)
It is also possible to have actions that are selectable directly from notifications, so you could have one or more notification that offer actions to control the direction, and simply allow the notification API to handle the job of appearing in front of the video.
Of course there are apps that manage to overlay their UI in front of other apps. Such an example is Thrutu, though you seem to be pushed for time, and getting such a solution working is not straightforward - see How do I implement a slide-out drawer on the Android's call screen?
I found my answer here. I need to start a service.
The library standout allows you to create floating applications(applications that go over other applications). I used this for a couple android applications I put on the market a while back. It makes creating the floating windows pretty simple. Check it out at:
http://forum.xda-developers.com/showthread.php?t=1688531
I have a ProgressDialog running, and I don't want that the screen goes off while this ProgressDialog is running. What should I use? Should I use a Windows Feature, or something like that?
I found the solution.
You should use something like that:
progressDialog.getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
That worked for me.
type the following line oncreate method
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Certain apps need to keep the screen turned on, such as games or movie apps. The best way to do this is to use the FLAG_KEEP_SCREEN_ON in your activity (and only in an activity, never in a service or other app component). For example:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
The advantage of this approach is that unlike wake locks (discussed in Keep the CPU On), it doesn't require special permission, and the platform correctly manages the user moving between applications, without your app needing to worry about releasing unused resources.
Another way to implement this is in your application's layout XML file, by using the android:keepScreenOn attribute:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
...
</RelativeLayout>
Using android:keepScreenOn="true" is equivalent to using FLAG_KEEP_SCREEN_ON. You can use whichever approach is best for your app. The advantage of setting the flag programmatically in your activity is that it gives you the option of programmatically clearing the flag later and thereby allowing the screen to turn off.
Note: You don't need to clear the FLAG_KEEP_SCREEN_ON flag unless you
no longer want the screen to stay on in your running application (for
example, if you want the screen to time out after a certain period of
inactivity). The window manager takes care of ensuring that the right
things happen when the app goes into the background or returns to the
foreground. But if you want to explicitly clear the flag and thereby
allow the screen to turn off again, use clearFlags():
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).
See more at: http://developer.android.com/training/scheduling/wakelock.html#screen
First off, yes I know that its not the intended behavior of the OS to disable the home button, but the application I am creating is for use on a collection of devices for use inside of a closed organization only and not public consumption. Secondly, I know that there are many similar questions and spotty answers everywhere, but no solution has been presented as of yet, so I felt it prudent (albeit somewhat self serving) to put the question out there one more time properly.
I am trying to simply create a splash screen that is also a remote network lock screen. When the device is remotely told to do so it will end the activity and the user can begin to use the device.
In the manifest this is set:
<activity android:name=".ActivityOne" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
In the Activity:
#Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
Combined these stop the home button, but seem to allow the status bar to remain visible. There are several obvious reasons why I would not want the status bar visible, but most importantly once a user 'pulls down' on the status bar to see the notifications area the home button becomes usable again and the whole exercise is circumvented.
I am primarily looking for an answer to this, condoned or a hack via code. If there is not a direct modification that can be done to the code set above, I would certainly appreciate and accept an answer that properly describes (preferably with code samples) any alternative logic that achieves the same goal. The alternatives can be to modify some system files or other lower level techniques.
As a final note, please know that we have SU / Root access for all of the devices.
Try this on onCreate method.
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
I am writing a 3d live wallpaper for android using the famous GLSurfaceView wrapper (http://www.rbgrn.net/content/354-glsurfaceview-adapted-3d-live-wallpapers)
When i open the preference screen to change wallpaper settings, the settings are displayed as transparent over my wallpaper preview. This is good because it lets me preview the settings changes I make. This works great, except for one problem: The live wallpaper is paused as long as the settings are on top of it!
How can i avoid my wallpaper pausing?
I came up with a workaround which could be useful, depending on how your wallpaper functions...
In my settings activity, whenever the user makes a change (e.g. moves a slider, checks a checkbox), I broadcast an Intent with the relevant settings information.
In the live wallpaper's onCreate method, I dynamically register a BroadcastReceiver to receive these settings events. I unregister this receiver in onDestroy.
When receiving a settings broadcast, I make the wallpaper draw a single frame with the new settings. As the settings activity is transparent, this immediately gives the user an idea of what the wallpaper will look like with the chosen settings.
Similarly, using this approach, you could schedule the wallpaper to animate for a few seconds after a settings change — or whatever is appropriate for your implementation.
I found this to be kinda annoying as well. I averted the issue altogether by simply calling finish() in my settings activity whenever a change is made. This allows for an instant, full preview of the wallpaper for the user. It only takes a click to go back into the settings to make another change and it makes for a rather nice user experience.
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
finish();
}
I used this in my line of wallpapers. Here is an example: