在我的应用程序中,在某些活动下必须保持屏幕始终处于活动状态(经典keepScreenOn)。
为了减少一点电池消耗,我想实现一个节能系统(类似于默认)类型:
在不活动10秒后,亮度降至最低(没有发送活动暂停或类似) 点击亮度恢复正常 等等...
可以实现这样的系统吗? 有一种自动计算空闲时间的方法吗? (因为否则我会创建一个从10到0的冷却,向每个用户点击10个......)
非常感谢
答案 0 :(得分:1)
尝试使用Settings.System.SCREEN_BRIGHTNESS将系统默认亮度设置为:
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" />
&#13;
答案 1 :(得分:0)
我认为可以使用WindowManager.LayoutParams:
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = 1F; // or whatever, but a float
getWindow().setAttributes(layoutParams);
值范围从0(暗)到1(亮)。低于0的值将重置默认的android值。
答案 2 :(得分: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);
答案 3 :(得分: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;
}