2014-12-04 145 views
0

在我的应用程序中,在某些活动下必须保持屏幕始终处于活动状态(classic keepScreenOn)。减少活动屏幕灯

为了减少一点点的电池消耗,我想实现的节能系统(类似于默认)类型:

10秒后不活动的,亮度都到了最小(而没有发送活动暂停或类似) 点击亮度恢复正常 等等......

是否可以实现这样的系统? 有一种自动的方式来计算空闲时间? (因为否则我将创建一个cooldwon从10到0,结转到10到每个用户的敲击...)

非常感谢您

+0

HT tp://stackoverflow.com/a/7658364/346232 – 2014-12-04 09:31:16

回答

0

我认为这是可能使用WindowManager.LayoutParams

WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); 
layoutParams.screenBrightness = 1F; // or whatever, but a float 
getWindow().setAttributes(layoutParams); 

值范围从0(暗)到1(亮)。低于0的值重置默认的android值。

+0

此功能仅在活动或设备上设置亮度? – Lele 2014-12-04 09:39:41

+0

我的'活动'只有我认为,因为您将使用'活动'窗口'对象... – shkschneider 2014-12-04 09:53:48

0

使用TimerTask处理时序

使用

@Override 
public void onUserInteraction() { 
    // TODO Auto-generated method stub 
    super.onUserInteraction(); 

    Log.d(TRCU.LOG_TAG,"User Interaction : "+userInteractionTimeout); 

} 

,以获取用户互动事件

,你也可以使用

  // Update brightness Instantly 
     WindowManager.LayoutParams lp =((Activity) context).getWindow().getAttributes(); 
     lp.screenBrightness =mCurrentValue/ 255.0f; 

     ((Activity) context).getWindow().setAttributes(lp); 
1

尝试使用设置调节亮度.System.SCREEN_BRIGHTNESS设置系统默认的bri ghtness为:

android.provider.Settings.System.putInt(getContentResolver(), 
 
android.provider.Settings.System.SCREEN_BRIGHTNESS,brightness /100.0f); // 0-255

,并添加这些许可的清单中:

<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> 
 
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

+0

此功能仅在活动或设备上设置亮度? – Lele 2014-12-04 09:39:26

0

先写出下列权限在manifest.xml文件

<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 

然后,当10秒通,运行下面的代码,以减少设备的亮度,它的较低水平:

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 0); //Set the system brightness 
android.view.WindowManager.LayoutParams w_lp = getWindow().getAttributes(); //Get the current window attributes 
w_lp.screenBrightness = progress/(float)255; //Set the brightness of this window 
getWindow().setAttributes(w_lp); //Apply attribute changes to this window 

,并在你需要知道如何使案件确保它等待10秒没有任何用户活动,实现上述代码的方式如下:

public class YOUR_ACTIVITY extends Activity { 

public static int x = 0; // must be static 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     run_thread(); 
} 

private void run_thread(){ 
    Thread thread = new Thread(new Runnable() { 

     @Override 
     public void run() { 
      while(x < 10){ 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       x++; 
      } 

    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 0); //Set the system brightness to it's lowest value 
    android.view.WindowManager.LayoutParams w_lp = getWindow().getAttributes(); //Get the current window attributes 
    w_lp.screenBrightness = progress/(float)255; //Set the brightness of this window 
    getWindow().setAttributes(w_lp); //Apply attribute changes to this window 

     } 
    }); 
    thread.start(); 
} 


@Override 
public boolean onTouchEvent(MotionEvent event) { 
    x = 0; 

    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255); //Set the system brightness back to it's full value 
    android.view.WindowManager.LayoutParams w_lp = getWindow().getAttributes(); //Get the current window attributes 
    w_lp.screenBrightness = progress/(float)255; //Set the brightness of this window 
    getWindow().setAttributes(w_lp); //Apply attribute changes to this window 

    return true; 
}