1

App有按鈕,在亮度之間切換的中間,但現在,如果按鈕被點擊了兩次,我嘗試點擊它再不會再起作用。有沒有人知道如何在第二次單擊按鈕之後單擊按鈕之前將屏幕亮度置於之前的狀態。 總的來說,點擊按鈕時,應用程序總是收到錯誤日誌。如果有人能夠解決這個問題,請提前致謝! 以下是錯誤日誌:拋出:SecurityException爲WRITE_SETTINGS許可,同時調整屏幕亮度

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

此外,您在這裏打破了良好的編程規則。 XXX的行爲(在本例中爲turnBrignessOff/On)應該與將其存儲在設置中無關。這應該是一種不同的方法,或者甚至是類,它知道如何做到這一點。你的職能打破了「做一件事,做正確的」基本規則。 (芹苴這是無關緊要的反正) –

+0

添加了例外,根據的是,它是與寫入設置,但我沒把權限的Android清單。 – Kenertj

回答

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"/>