So I am fairly new to the world of coding and I am doodling with a little private learning project.
I have made a simple web browser based on WebView for a embedded android 7.1 ELOtouch device.
I have found plenty of articles online on how to turn the screen on/off etc but never really managed to make it work.
What I am trying to do is that the screen dims down to the lowest level after xx amount of time, let’s say 5min. And only dims back up to a defined level upon user touch/screen input.
The unit is always on and don’t have any form for advanced screen adjustments in settings, so as I see it, it has to be done programmatically.
Thankful for and advice or guidance.
Attached one of my sources:
How to change screen timeout programmatically?
To change screen brightness by user touch, you could do like this:
In AndroidManifest.xml file, add this line:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
And in some place of activity class file:
Settings.System.putInt(this.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, 80);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness =0.8f;
getWindow().setAttributes(lp);
startActivity(new Intent(this,DummyActivity.class));
Note: when setting up brightness,the modification doesn't take effect immediately, to solve this problem,just start another blank dummy activity and finish it in Oncreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish();
}
Don' forget to include DummyActivity in AndroidManifest.xml.
Edited:
To dim screen after some time, the basic logic is same, and maybe you should create a Timer(TimerTask).
Hope this is helpful!
Related
I made an field guide app for biology. It does tons of stuff, for example, recordings of wildlife sounds, and it is made to run the whole day with a single battery charge.
Since it is intended to be run a whole day, I cannot keep the screen on all the time. So naturally the screen goes off. Then, the lock screen kicks in, blocking the app. Then you need to enter the pin/pattern/whatever and you then miss the opportunity of a precious recording. Dozens of times per day. And in general the user is using my app in the wild, where there is much less risk of theft.
So I present the user an option in the preference section of my app to turn lock screen off while using it. There is no problem with this (see below for the code I made), except that when I switch to a secondary activity the lock screen appears. It is not truly a "lock screen", in the sense that it shows the back button that when you press it the lock screen disappears. But still, a pain when you're in a hurry. I want no lock screen. At all.
Interestingly, when I switch back to the primary activity from a secondary, no lock screen is shown...
This is the way I found to (partially) disable the lock screen (executed in each activity act):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
act.setShowWhenLocked(true);
act.setTurnScreenOn(true);
((KeyguardManager) act.getSystemService(Context.KEYGUARD_SERVICE)).requestDismissKeyguard(act, null);
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
// act.setInheritShowWhenLocked(true); // makes no difference?
} else {
Window window = act.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
Of course, I also have in the Manifest:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
It turns out that the requestDismissKeyguard was causing the Lock Screen to be activated instead of deactivated (??!!). So I removed the line
((KeyguardManager) act.getSystemService(Context.KEYGUARD_SERVICE)).requestDismissKeyguard(act, null);
and now everything works just fine.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am developing custom lockscreen app.its working fine in below 4.0 but above 4.0,when we press home button the app stops.is there any solution for this no apps will stop when pressing home button untill unlocking the screen.(like go locker app)
Another way to develop a LockScreen App is by using Views, let me explain it.
First of all you can "disable" in some devices the System lock screen by disabling the KEYGUARD:
((KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock("IN").disableKeyguard();
You should put this line of code in your Service.
After that you can launch an activity every time the screen goes off:
public class AutoStart extends BroadcastReceiver {
public void onReceive(Context arg0, Intent arg1) {
if(arg1.getAction().equals("android.intent.action.SCREEN_OFF")) {
Intent localIntent = new Intent(arg0, LockScreen.class);
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent.addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
arg0.startActivity(localIntent);
}
}
}
If you read the documentation for WindowManager.LayoutParams.TYPE_SYSTEM_ERROR it explains that is a type of internal system error windows, appear on top of everything they can. In multiuser systems shows only on the owning user's window.
So now you have an activity on top of everything, but a press in HOME button will exit the activity.
Here is where the Views make their appearance. You can inflate a view from a layout resource and add it to the WindowManager as a TYPE_SYSTEM_ERROR, so will be on top of everything. And since you can control when to remove this View, the best place is in onDestroy of your Activity, because pressing the HOME button will only pause your activity, and the view will still be visible.
public WindowManager winManager;
public RelativeLayout wrapperView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
this.winManager = ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE));
this.wrapperView = new RelativeLayout(getBaseContext());
getWindow().setAttributes(localLayoutParams);
View.inflate(this, R.layout.lock_screen, this.wrapperView);
this.winManager.addView(this.wrapperView, localLayoutParams);
}
public void onDestroy()
{
this.winManager.removeView(this.wrapperView);
this.wrapperView.removeAllViews();
super.onDestroy();
}
To avoid the notification bar of showing I added the flags FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL | FLAG_LAYOUT_IN_SCREEN to consume all pointer events.
Not forget to add these two lines to your manifest:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
From here you just need to add the logic of your Lock Screen app to let the user use his smartphone :)
A custom launcher is basically an app (you can make it behave like a grid, list, implement your own drag and drop etc) then, you only need to add these lines to the intent filter of the main activity, with this done, after you install your app and press the home button your app will appear in the list of available homescreens.
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
What i cant find is a way to replace the lock screen, and hacks like disabling the lock screen on the phone and using an activity in a custom launcher isn't actually replacing the lockscreen ^^
You can use the below method to disable the Home key in android :
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
I am developing on a Samsung Galaxy S4 5.0 and what worked for me was simply changing getWindow().setFlags(..) to getWindow().addFlags(..)
I think first of all you should ask yourself if you really want to hijack the home key. Sometimes you may want it. But I think placing the app on the Android lock screen, letting the home key act normally and letting the underlying Android lock screen take care of password-protecting the device is what you actually want in a lot of cases (unless you want to change the way this is done by default).
Bottom line, letting an app be displayed on the Android lock screen comes pretty close to writing your own custom lock screen. And is decidedly easier since you don't have to manage passwords yourself. Not to mention it's safer and more reliable since you don't hijack the home key.
I did it like this and it works very well. You can see the details here:
show web site on Android lock screen
The question is about displaying a website on the lock screen, since that's what I was interested in, but the answer is more general, it works with any app.
You can see here an app that's on Google Play and has been written like this:
https://play.google.com/store/apps/details?id=com.a50webs.intelnav.worldtime
I want to make a button that float on homescreen and also work if any other activity is in running.
i have tricked many ideas and also google vary much but i can't get still any solution..
for example i have tried to make my application transparent and full screen and as a system alert flag from this stackoverflow question: Creating a system overlay window (always on top)
#Override
protected void onStop(){
super.onStop();
Intent intent = new Intent(this, ClassNameOfYourActivity.class);
startActivity(intent);
}
and also tried to start my activity at every movement but it is very bad idea like this:
can't get any idea of doing this...Hopefully requested to share me any idea or example if you have experienced about this...
thanzzzz in advance..
I know that this question has been asked a lot of times but it has never been answered satisfactorily.
My problem is the following:
I have an activity which prevents the screen from turning off for a predefined amount of time.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
When the predefined time is over I show a dialog with a countdown to inform the user that the display will turn off in 10 seconds if he doesnt press "cancel".
I managed to turn off the display but the phone always switches into StandBy-Mode.
For switching off I used:
Window mywindow = getWindow();
WindowManager.LayoutParams lp = mywindow.getAttributes();
lp.screenBrightness = 0.0f;
mywindow.setAttributes(lp);
Is there any possibility to completely darken the display without going to StandBy-Mode (which pauses the activity).
My goal is that the user should be able to just tap the display to brighten up the screen again. So the activity has to remain in an active state.
A similar question has been asked here.
Since this question is almost a year old I am hoping that maybe somebody managed to this in the mean time.
Lots of greetings
Siggy
Seems like it isn't possible to turn off the screen AND reactivate just by touching the display.
My new approach now:
private WakeLock screenWakeLock;
PowerManager pm = PowerManager.getSystemService(Context.POWER_SERVICE);
screenWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"screenWakeLock");
screenWakeLock.acquire();
The PARTIAL_WAKE_LOCK keeps the CPU running but allows the display to shut down.
When power or home button is pressed the display turns on again and the activity becomes visible again (without having to "slide-to-unlock" or sth. else).
Don't forget to release the screenWakeLock.
In my case I did it in the onResume() of the activity:
if (screenWakeLock != null) {
if(screenWakeLock.isHeld())
screenWakeLock.release();
screenWakeLock = null;
}
Maybe this helps someone with a similar problem in the future.
Note : i wasn't able to work with WAKELOCK
I have figured a workaround which involves changing SCREEN_OFF_TIMEOUT. Use the below
code to achieve it.
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, 10);
this sets 10 milliseconds as timeout for screen_timeout SYSTEM-WIDE .
Now you might be troubled by SYSTEM-WIDE changes brought upon by this. To work around that you can get the default screen_timeout_time and save it to a variable then set back the System's SCREEN_OFF_TIMEOUT at finish() of your activity.
Before setting 10ms as our screen_timeout get SCREEN_OFF_TIMEOUT,
int defaultScreenTimeout= android.provider.Settings.
System.getInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,-1);
Now when you have finished with your changes or when your activity ends you may set the SCREEN_OFF_TIMEOUT back .
#Override
public void finish(){
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, defaultScreenTimeout);
super.finish();
}
I have an air for android app that has event listeners for ACTIVATE and DEACTIVATE, inside the activate I tell the screen to stay awake and in the deactivate I tell it to go back to normal like so :
stage.addEventListener(Event.DEACTIVATE, deactivateHandler);
stage.addEventListener(Event.ACTIVATE, activateHandler);
protected function deactivateHandler(event:Event):void{
SFX.disableSound();
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.NORMAL;
}
protected function activateHandler(event:Event):void{
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
}
But the screen will stay awake at all times even when on the android home screen unless you force close the app... any ideas?
Thanks
I had this exact problem. My app was able to keep the screen on by setting systemIdleMode to SystemIdleMode.KEEP_AWAKE, and it would force the screen to stay on. However, when the app tried to set the systemIdleMode back to SystemIdleMode.NORMAL, so the screen could turn off, the screen was still staying on.
What turned out to be the problem in my case is was a missing android permission. I had already added this permission to my app XML file, so that i could use the keep-alive function:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Turns out this is not the only permission you need. I added this permission also:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
And suddenly my app was able to let the screen be turned off again.
You could try using this in your java code:
// Gets one of the views visible on the screen and sets keepScreenOn to true.
// This means the screen will stay on as long as the specified view is visible.
this.findViewById(R.id.viewId).setKeepScreenOn(true);
Or you could put android:keepScreenOn="true" in your layout.