【问题标题】:Aplication Closes but one of the functions dont remain应用程序关闭,但其中一项功能不保留
【发布时间】:2015-09-22 21:12:04
【问题描述】:

美好的一天!

我正在做一个具有省电功能的项目,它会禁用 wifi,屏幕亮度尽可能低,并且在振铃模式下振动。但每次我关闭(按下后退按钮)或按下导致另一个活动的按钮时,亮度都会升高(用户偏好)。知道为什么以及如何使它保持在低水平吗?

这些是我的代码。

btnBattSave.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
    wifiManager.setWifiEnabled(false);

    mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);

    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.screenBrightness = 0;
    getWindow().setAttributes(layoutParams);



    Toast.makeText(getApplicationContext(),
            "Wifi Turned Off \r \n "
            + "Silent Mode On \r \n "
            + "Brightness Turned Low",
            Toast.LENGTH_LONG).show ();

   }});
 }      
    public void send(View arg1)
    {
        Intent i = new Intent(this, InformationActivity.class);         
        startActivity(i);
    }
    public void exit(View view)
    {
          System.exit(0);
    }

我也表明了意图和退出。

谢谢!

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以通过以下方式做到这一点

     private int brightness;
     private ContentResolver mCR;
     private Window window;
    

    在 onCreate 方法中初始化这个

     mCR = getContentResolver();
     window = getWindow();
    

    在 onclickListner() 中添加这个

    //add here your value in place of 10  
    Settings.System.putInt(mCR, Settings.System.SCREEN_BRIGHTNESS, 10);
    WindowManager.LayoutParams layoutpars = window.getAttributes();
    layoutpars.screenBrightness = brightness / (float)255;
    window.setAttributes(layoutpars);
    

    【讨论】:

    • 它就像一个魅力谢谢!顺便问一下,它是如何工作的,ContentResolver 和 Window 的用途是什么?
    • 在当前屏幕中设置的窗口和用于在设置中设置值的 ContetResolver 并将其标记为已接受,如果这对您有帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    相关资源
    最近更新 更多