应用程序中间的按钮在亮度之间切换,但是现在,如果按钮被点击两次并且我再次尝试点击它,则它不再起作用。有没有人知道如何在第二次单击按钮后单击按钮之前将屏幕亮度置于先前状态。 总的来说,点击按钮时应用程序会不断收到错误日志。如果有人能解决这个问题,请提前致谢! 这是错误日志:
W/System.err: java.lang.SecurityException: com.example.kenert.allinoneapp was not granted this permission: android.permission.WRITE_SETTINGS.
W/System.err: at android.os.Parcel.readException(Parcel.java:1684)
W/System.err: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
W/System.err: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
W/System.err: at android.content.ContentProviderProxy.call(ContentProviderNative.java:646)
W/System.err: at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:1595)
W/System.err: at android.provider.Settings$System.putStringForUser(Settings.java:1966)
W/System.err: at android.provider.Settings$System.putIntForUser(Settings.java:2071)
W/System.err: at android.provider.Settings$System.putInt(Settings.java:2065)
W/System.err: at com.example.kenert.allinoneapp.ScreenFlashlight$override.turnBrightnessOff(ScreenFlashlight.java:60)
W/System.err: at com.example.kenert.allinoneapp.ScreenFlashlight$override.screenFlashButtonClicked(ScreenFlashlight.java:28)
W/System.err: at com.example.kenert.allinoneapp.ScreenFlashlight$override.access$dispatch(ScreenFlashlight.java)
W/System.err: at com.example.kenert.allinoneapp.ScreenFlashlight.screenFlashButtonClicked(ScreenFlashlight.java:0)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
W/System.err: at android.view.View.performClick(View.java:5637)
W/System.err: at android.view.View$PerformClick.run(View.java:22429)
W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:154)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
以下是该类的代码:
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
public class ScreenFlashlight extends AppCompatActivity {
private boolean brightnessOnOff;
private ImageButton screenFlashOnOffButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_flashlight);
screenFlashOnOffButton = (ImageButton) findViewById(R.id.screenFlashOnOffButton);
brightnessOnOff = false;
}
public void screenFlashButtonClicked(View view) {
try {
if (brightnessOnOff) {
turnBrightnessOff();
} else{
brightnessOnOff = true;
turnBrightnessOn();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void turnBrightnessOn() {
try {
screenFlashOnOffButton.setImageResource(R.drawable.screenonbutton);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, 255);
//Screen refresh to add brightness
int brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = (float) brightness / 255;
getWindow().setAttributes(lp);
} catch (Exception e) {
e.printStackTrace();
}
}
private void turnBrightnessOff() {
try {
screenFlashOnOffButton.setImageResource(R.drawable.screenoffbutton);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, 100);
//Refresh screen
int brightnesss = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
WindowManager.LayoutParams lpp = getWindow().getAttributes();
lpp.screenBrightness = (float) brightnesss/100;
getWindow().setAttributes(lpp);
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
我用这个:
public void onClick(View arg0) {
int curBrightnessValue=android.provider.Settings.System.getInt(
getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS);
int SysBackLightValue = curBrightnessValue-25;
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
}
不要忘记许可:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>