How to keep screen on with ProgressDialog? - android

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

Related

Distorted screen with certain intent/phone combo [duplicate]

The app I'm currently building has the requirement that the app has to prevent the OS to take a screenshot of the app when it's being pushed into the background for security reasons. This way it won't be able to see the last active screen when switching between apps.
I'm planning to put this functionality in the application class's onPause method, but first I need to find out how I can achieve this functionality.
So is there anybody out there, that has a clue how to fix this?
Try FLAG_SECURE:
public class FlagSecureTestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.main);
}
}
This definitely secures against manual screenshots and automatic screenshots from the ICS recent-tasks history. It also secures against screen recording (e.g., apps using the media projection APIs).
UPDATE: it also secures against Now On Tap or other assistants on Android 6.0; they will not get access to the details of widgets and containers in your UI if the user brings up the assistant.
UPDATE #2: however, not everything in the activity will be protected. Any pop-up windows — Dialog, Spinner, AutoCompleteTextView, action bar overflow, etc. — will be insecure. You can fix the Dialog problem by calling getWindow() on it and setting FLAG_SECURE. The rest... gets tricky. See this blog post for more.
Be careful about using WindowManager.LayoutParams.FLAG_SECURE, on some devices (verified on Samsung Galaxy ACE, e.g. GT-S5830) this will make the view scrambled. Looks like a Samsung specific bug. I recommend the following:
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
}
This is what a scrambled screen looks like:
This is working properly on ICS Samsung phones though, so I'm assuming problem is isolated to Gingerbread devices (or older).
The solution provided by CommonsWare continues to be valid also in Lollipop.
Just a note, if you want to continue to not see snapshots in recent list for the entire app, ALL the implemented activities should specify in the onCreate() method the flag getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
before setContentView();
Otherwise a snapshot in the recent list will show the first activity without the flag if the user navigated through it.
In case if someone is looking for a solution in which the app must secure (screen overlay) when the app is background or stick of all running app and the in-app app should allow screenshot.
Try Below:-
#Override
protected void onResume() {
super.onResume();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
#Override
protected void onPause() {
super.onPause();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
}
Here is a solution for hiding content of an app by covering it with a splash screen when the app is put into the background. This is not using the FLAG_SECURE technique, I simply override the onPause and onResume methods of the screens and modify the view to show one that covers everything in the back.
https://stackoverflow.com/a/52976001/6686912
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
this worked for me, it prevents from taking screenshot and also any inbuilt or third party recording application from recording screen.
This is work for me after adding these line into the onCreate before setContentView of every activity.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.activity_notification);
As of Android 13, there is a new way to prevent a screenshot being taken for the recent apps list, while still allowing the user to take screenshots while using the app: https://developer.android.com/reference/android/app/Activity#setRecentsScreenshotEnabled(boolean)

keep android screen from dimming while activity is running

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);

How do I prevent Android taking a screenshot when my app goes to the background?

The app I'm currently building has the requirement that the app has to prevent the OS to take a screenshot of the app when it's being pushed into the background for security reasons. This way it won't be able to see the last active screen when switching between apps.
I'm planning to put this functionality in the application class's onPause method, but first I need to find out how I can achieve this functionality.
So is there anybody out there, that has a clue how to fix this?
Try FLAG_SECURE:
public class FlagSecureTestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.main);
}
}
This definitely secures against manual screenshots and automatic screenshots from the ICS recent-tasks history. It also secures against screen recording (e.g., apps using the media projection APIs).
UPDATE: it also secures against Now On Tap or other assistants on Android 6.0; they will not get access to the details of widgets and containers in your UI if the user brings up the assistant.
UPDATE #2: however, not everything in the activity will be protected. Any pop-up windows — Dialog, Spinner, AutoCompleteTextView, action bar overflow, etc. — will be insecure. You can fix the Dialog problem by calling getWindow() on it and setting FLAG_SECURE. The rest... gets tricky. See this blog post for more.
Be careful about using WindowManager.LayoutParams.FLAG_SECURE, on some devices (verified on Samsung Galaxy ACE, e.g. GT-S5830) this will make the view scrambled. Looks like a Samsung specific bug. I recommend the following:
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
}
This is what a scrambled screen looks like:
This is working properly on ICS Samsung phones though, so I'm assuming problem is isolated to Gingerbread devices (or older).
The solution provided by CommonsWare continues to be valid also in Lollipop.
Just a note, if you want to continue to not see snapshots in recent list for the entire app, ALL the implemented activities should specify in the onCreate() method the flag getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
before setContentView();
Otherwise a snapshot in the recent list will show the first activity without the flag if the user navigated through it.
In case if someone is looking for a solution in which the app must secure (screen overlay) when the app is background or stick of all running app and the in-app app should allow screenshot.
Try Below:-
#Override
protected void onResume() {
super.onResume();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
#Override
protected void onPause() {
super.onPause();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
}
Here is a solution for hiding content of an app by covering it with a splash screen when the app is put into the background. This is not using the FLAG_SECURE technique, I simply override the onPause and onResume methods of the screens and modify the view to show one that covers everything in the back.
https://stackoverflow.com/a/52976001/6686912
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
this worked for me, it prevents from taking screenshot and also any inbuilt or third party recording application from recording screen.
This is work for me after adding these line into the onCreate before setContentView of every activity.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.activity_notification);
As of Android 13, there is a new way to prevent a screenshot being taken for the recent apps list, while still allowing the user to take screenshots while using the app: https://developer.android.com/reference/android/app/Activity#setRecentsScreenshotEnabled(boolean)

Disable Home Button and Have Application Full Screen with No Title (no status bar, no title) in Android Froyo

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);

Enable or disable the PatternLock screen from code

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.

Categories

Resources