i would like to use following code to turn off Button back that i got from https://stackoverflow.com/a/4937448/1218762
final Window win = getWindow();
final WindowManager.LayoutParams winParams = win.getAttributes();
winParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
//set screen brightness to the lowest possible
winParams.screenBrightness = 0.01f;
if (Build.VERSION.SDK_INT < 8) {
// hack for pre-froyo to set buttonBrightness off
try {
Field buttonBrightness = winParams.getClass().getField(
"buttonBrightness");
buttonBrightness.set(winParams, 0);
} catch (Exception e) {
e.printStackTrace();
}
} else {
winParams.buttonBrightness = 0;
}
win.setAttributes(winParams);
Reference : visit Night Mode where you you can turn off Button Lights in Service , i know Services don't have Window but how is it possible ?
Thank You.
One of the simple way I would suggest you is to broadcast some kind of turning-off message from your Service to your Activity and perform the required actions inside onReceive method of BroadcastReceiver.
Related
I want to get the real brightness value from the background. I have tried several ways:
1.
curBrightnessValue =android.provider.Settings.System.getInt(
getContext().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS);
But if the screen brightness is on Auto mode the value remain constant.
Reading the sys/class/backlight/brightness/
This is a good way but I want a way without reading a file.
Use the following code to get the brightness of the background (This will also allow you to change the value of brightness if you wish to):
Settings.System.putInt(
cResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
brightness =
Settings.System.getInt(
cResolver,
Settings.System.SCREEN_BRIGHTNESS);
System.out.println("Current Brightness level " + brightness);
To my knowledge, it cannot be done any other way in Auto mode. See this answer.
Method 1 as described using Settings.System.getInt() in auto mode doesn't work for older Android versions like 'N'. But it works for 'P' and it's the same value as that in the file /sys/class/backlight/panel0-backlight/brightness.
Some sample code I tried in Processing
import android.provider.Settings; // for global system settings
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Activity act;
Context context;
void setup() {
act = this.getActivity();
context = act.getApplicationContext();
}
void draw() {
text("brightness = " + getBrightness());
}
float getBrightness() {
float brightness;
if(!Settings.System.canWrite(context)) {
// Enable write permission
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
context.startActivity(intent);
} else {
// Get system brightness
Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); // enable auto brightness
brightness = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, -1); // in the range [0, 255]
}
return brightness;
}
I am looking for how we change the vibrate settings in Jelly Bean. I found that all the pre-JB vibrate settings have been deprecated, but don't see any new AudioManager.[change the vibrate settings] code anywhere. There is a setting "Vibrate when ringing" which I would like to know how to play with.
Thanks for you help.
In android4.1 you can use this to control "vibrate & ringing"
Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, enable ? 1 : 0);
From the Documentation:
This method is deprecated.
Applications should maintain their own vibrate policy based on current ringer mode that can be queried via getRingerMode().
Seems like Google wants Vibration settings to be handled by each app for itself on an app to app basis, adjusting it's settings by querying getRingerMode().
Settings.System.VIBRATE_WHEN_RINGING is declared with #hide annotation so you may have troubles to use it in Android Studio.
So maybe you have to replace Settings.System.VIBRATE_WHEN_RINGING by its value : "vibrate_when_ringing"
It is annoying that it is declared with the #hide annotation, because it means that Google don't want external developpers using it...
This issue has plagued me for a couple of days now. Finally got it working. Make sure that you have the right permissions as well.
uses-permission android:name="android.permission.WRITE_SETTINGS"/
protected void onCreate(Bundle savedInstanceState) {
audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
}
Then in my on click listener I utilized the following:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (vibrate == 1) {
try {
Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 1);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on ring:
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on notify
boolean vibrateWhenRinging;
vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1);
Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "System vibrate error", Toast.LENGTH_LONG).show();
}
} else {
try {
Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 0);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on ring:
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on notify
boolean vibrateWhenRinging;
vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1);
Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "System vibrate error in else statement", Toast.LENGTH_LONG).show();
}
}
}
I'm simply trying to toggle auto brightness on and off.
I started with this code (inside the onCreate method)
final ToggleButton autoBrightToggle = (ToggleButton) findViewById(R.id.brightToggle);
// display auto brightness state
final ToggleButton autoBrightToggle = (ToggleButton) findViewById(R.id.autoToggle);
autoOnOrOff.setText(String.valueOf(getAutoBrightnessMode()));
autoBrightToggle.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (autoBrightToggle.isChecked()) {
setAutoBright(true);
} else {
setAutoBright(false);
}
}
}); // end anonymous OnClickListener function
// toggle the brightness mode
private void setAutoBright(boolean mode) {
if (mode) {
Settings.System.putInt(cr, SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
autoOnOrOff.setText(String.valueOf(getAutoBrightnessMode()));
} else {
Settings.System.putInt(cr, SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
autoOnOrOff.setText(String.valueOf(getAutoBrightnessMode()));
}
}
Which doesn't seem to work. The setAutoBrightnessMode() method is also called again in onResume() but with the same non-results.
Anyway, I'm sorry if someone feels this question is redundant but the other posts did not get me where I need to go!
(FWIW - I'm testing this on my old Droid X and my Galaxy Nexus, not the Emulator)
EDITED - UPDATE ON THIS:
I'm 99% sure now that I am not seeing any changes to the Auto-Brightness mode reflected in the Settings panel and desktop widgets - even though I may actually be changing it's value.
part of the problem is that I don't know how exactly to determine if Auto-Brightness is on or not!
For instance, does the screen quickly and visibly change? I've been expecting immediate visible changes in brightness according to environment - but perhaps the changes are subtle? and over a longer period? or perhaps it takes 30 seconds or more of environment change before brightness changes?
Can someone suggest how I can track this? I've tried querying the Settings.System.SCREEN_BRIGHTNESS_MODE constant - hooking this method up to a textfield:
private int getAutoBrightnessMode() {
try {
int brightnessMode = Settings.System.getInt(cr, SCREEN_BRIGHTNESS_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
int brightnessMode = -10000;
}
return brightnessMode;
}
But it always reads 0, even after an onResume(). :-((
I know this is a simple procedure, but I'm trying to learn this stuff on my own, and have had almost no formal CS training... So all I can say is I'm very frustrated by this and feel like I've worked myself into a corner and at this point I'm so annoyed I can't think straight anymore.
So help would be great.
I use following approach in my application. Tested on HTC Desire HD and pair of noname chinese tablets.
Add to manifest permission:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
And use below code to toggle auto brightness. There is one trick in the code: we need to "refresh" brightness of app manually, because it doesn't changes automatically. May be it is the problem in your case.
void setAutoBrightness(boolean value) {
if (value) {
Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
} else {
Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
}
// After brightness change we need to "refresh" current app brightness
if (isChecked) {
refreshBrightness(-1);
} else {
refreshBrightness(getBrightnessLevel());
}
}
private void refreshBrightness(float brightness) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
if (brightness < 0) {
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
} else {
lp.screenBrightness = brightness;
}
getWindow().setAttributes(lp);
}
int getBrightnessLevel() {
try {
int value = Settings.System.getInt(getContentResolver(), SCREEN_BRIGHTNESS);
// convert brightness level to range 0..1
value = value / 255;
return value;
} catch (SettingNotFoundException e) {
return 0;
}
}
I am making a home application and I think that it will be suitable if I use a fullscreen and not show the status bar. So now I want to be able to open or expand the status bar with a button on the menu, similar to the way some default home applications have in the menu. I know its possible since the default home does it. Is this done through an intent? If so can I have the code for it. If not well then I would appreciate it if you guys showed me how. Thanks!
See if this helps and let me know...
try{
Object service = getSystemService("statusbar");
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method expand = statusbarManager.getMethod("expand");
expand.invoke(service);
}
catch(Exception ex){
....
}
uses permission : "android.permission.EXPAND_STATUS_BAR";
The code below works for me:
boolean shown = true;
private void showHide() {
Window w = this.getWindow();
if(shown)
{
w.setFlags(0,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else
{
w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
shown=!shown;
}
This one worked for me:
manifest:
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
Code:
// https://gist.github.com/XinyueZ/7bad2c02be425b350b7f requires permission: "android.permission.EXPAND_STATUS_BAR"
#SuppressLint("WrongConstant", "PrivateApi")
#JvmStatic
fun setExpandNotificationDrawer(context: Context, expand: Boolean) {
try {
val statusBarService = context.getSystemService("statusbar")
val methodName =
if (expand)
if (Build.VERSION.SDK_INT >= 17) "expandNotificationsPanel" else "expand"
else
if (Build.VERSION.SDK_INT >= 17) "collapsePanels" else "collapse"
val statusBarManager: Class<*> = Class.forName("android.app.StatusBarManager")
val method: Method = statusBarManager.getMethod(methodName)
method.invoke(statusBarService)
} catch (e: Exception) {
e.printStackTrace()
}
}
However, someone wrote it doesn't work on all devices and Android versions (here), so I wrote a request to add official API for this, here.
I'm developing an Android application that might be used at night. Therefor, I need to turn off the buttons' backlight. How can I do this? On my own phone the backlight turns off after a while, but on the Motorola Droid I don't think this happens.
I'm using a wakelock to keep the screen on. Should I use another flag or how can I do this?
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, WAKE_LOCK_TAG);
mWakeLock.acquire();
Thank you very much!
//Kaloer
There is a hack:
private void setDimButtons(boolean dimButtons) {
Window window = getWindow();
LayoutParams layoutParams = window.getAttributes();
float val = dimButtons ? 0 : -1;
try {
Field buttonBrightness = layoutParams.getClass().getField(
"buttonBrightness");
buttonBrightness.set(layoutParams, val);
} catch (Exception e) {
e.printStackTrace();
}
window.setAttributes(layoutParams);
}
I see that this is an old question that was mostly answered in a comment link, but to make it clear to anyone else who comes across this question, here's my own answer.
It's built-in since API 8. (doc)
float android.view.WindowManager.LayoutParams.buttonBrightness
This is a somewhat modified/simplified version of what I'm using in one of my apps (excluding irrelevant code). The inner class is required to prevent a crash at launch on older platforms that don't support it.
private void nightMode() {
Window win = getWindow();
LayoutParams lp = win.getAttributes();
if (prefs.getBoolean("Night", false))
changeBtnBacklight(lp, LayoutParams.BRIGHTNESS_OVERRIDE_OFF);
else changeBtnBacklight(lp, LayoutParams.BRIGHTNESS_OVERRIDE_NONE);
win.setAttributes(lp);
}
private void changeBtnBacklight(LayoutParams lp, float value) {
if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
try {
new BtnBrightness(lp, value);
} catch (Exception e) {
Log.w(TAG, "Error changing button brightness");
e.printStackTrace();
}
}
}
private static class BtnBrightness {
BtnBrightness(LayoutParams lp, float v) {
lp.buttonBrightness = v;
}
}
AFAIK, there is no API to control the backlight of the buttons -- sorry!