總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年2月1日 星期三

PowerManager ~ 更改螢幕背光 ~ getWindow().setAttributes











MainActivity.java
package com.tsots.ScreenBrightness;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements Runnable
{
 Context context = MainActivity.this;
 Button b1 ;
 EditText et1;
 TextView str_start_backlight, str_end_backlight;
 SeekBar start_BackLight, end_BackLight;
 PowerManager.WakeLock mWakeLock;
 PowerManager mPowerManager; 
 String str_et; 
 boolean ifLocked = false;
 float f_start_backlight, f_end_backlight, f_backlight_change;
 int time;
 
 @Override 
 public void onCreate(Bundle savedInstanceState) 
 { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main);
  
  b1 = (Button) findViewById (R.id.b1);
  et1 = (EditText) findViewById (R.id.et1); 
  start_BackLight = (SeekBar) findViewById(R.id.start_backlight);
  end_BackLight = (SeekBar) findViewById(R.id.end_backlight);
  str_start_backlight = (TextView) findViewById(R.id.str_start_backlight);
  str_end_backlight = (TextView) findViewById(R.id.str_end_backlight);
  mPowerManager = (PowerManager) getSystemService (Context.POWER_SERVICE);
  final WindowManager.LayoutParams layoutParams = getWindow().getAttributes();  
  
     mWakeLock = mPowerManager.newWakeLock 
     ( 
      PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "BackLight" 
     ); 
     et1.setText("1");
     str_start_backlight.setText("0.1");
     str_end_backlight.setText("0.1");
     
     start_BackLight.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 
  {   
   public void onProgressChanged(SeekBar arg0, int arg1,boolean arg2) 
   {   
    float BackLightValue = (float) arg1 / 100;
    //避免螢幕亮度的值為0.0, 會全暗無法操作
    if(arg1 > 0.0)
    {
     str_start_backlight.setText(String.valueOf(BackLightValue));      
     layoutParams.screenBrightness = BackLightValue;
     getWindow().setAttributes(layoutParams);
    }
   }   
   public void onStartTrackingTouch(SeekBar arg0) 
   { }   
   public void onStopTrackingTouch(SeekBar arg0) 
   { }
  });
     end_BackLight.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 
  {   
   public void onProgressChanged(SeekBar arg0, int arg1,boolean arg2) 
   {   
    float BackLightValue = (float) arg1 / 100;
    if(arg1 > 0.0)
    {
     str_end_backlight.setText(String.valueOf(BackLightValue)); 
    }
   }   
   public void onStartTrackingTouch(SeekBar arg0) 
   { }   
   public void onStopTrackingTouch(SeekBar arg0) 
   { }
  });
  
  b1.setOnClickListener(new Button.OnClickListener()
  {
   public void onClick(View v) 
   {
    f_start_backlight = Float.valueOf(str_start_backlight.getText().toString());
    f_end_backlight = Float.valueOf(str_end_backlight.getText().toString());
    
    time = Integer.valueOf(et1.getText().toString());
    f_backlight_change = (f_end_backlight-f_start_backlight)/(time);
    Toast.makeText(context, "每秒變化量: "+f_backlight_change, Toast.LENGTH_SHORT).show();
    Thread thread = new Thread(MainActivity.this);
    thread.start();     
   }   
  });
 } 
   
 @Override 
 protected void onResume() 
 {  
  wakeLock(); 
  super.onResume(); 
 } 
   
 @Override 
 protected void onPause() 
 {
  wakeUnlock(); 
  super.onPause();
 } 

 private void wakeLock()
 { 
  if (!ifLocked) 
  { 
   ifLocked = true;
   mWakeLock.acquire();
  }
 } 

 private void wakeUnlock() 
 { 
  if (ifLocked) 
  { 
   mWakeLock.release(); 
   ifLocked = false;
  }
 } 
 
 public void run()
    {
     try
        { 
      for(int i=0; i
   {
       //設定每隔一秒做一次變化
       Thread.sleep(1000);
       //由於thread無法直接改變Activity的widget, 需借助Handler
       handler.sendEmptyMessage(0);      
   }           
        }
        catch (Exception e)
        {
         System.out.println(e);
        }   
    }         
 
 /*
  * 設定螢幕背光
  */
 Handler handler = new Handler()
 {
  public void handleMessage(Message msg)
  {
   final WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
   f_start_backlight = f_start_backlight+f_backlight_change;
   layoutParams.screenBrightness = f_start_backlight;
   getWindow().setAttributes(layoutParams);
  }
 };
}//

沒有留言:

張貼留言