手机设置的亮度调节功能

main.xml 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

  <Button android:id="@+id/btn"      //控制跳出亮度调节的按钮
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="btn" />"

</LinearLayout>

自己定义的亮度调节对话框

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   <CheckBox android:id="@+id/cb"
                   android:layout_height="wrap_content"
                   android:layout_width="220dip"    //用于控制自动调节和手动调节
                   android:text="我叫小治"    />
   
     <SeekBar

                 android:id="@+id/seek"

                android:layout_width="fill_parent"

                 android:layout_height="wrap_content"

                 android:layout_margin="10dip"  //拖动seekbar手动调节

                 android:max="500"/>
   

</LinearLayout>

亮度调节的几个函数

1.判断当前是否自动调节亮度

public static boolean isAutoBrightness(ContentResolver aContentResolver) {   

     boolean automicBrightness = false;   

     try{       

     automicBrightness = Settings.System.getInt(aContentResolver,               

     Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;  

      }

     catch(SettingNotFoundException e)

     {      

      e.printStackTrace(); 

       }   

     return automicBrightness;}//判断是否自动调节
   
   
    2.获取当前的亮度,经过试验,只能获得自动调节的亮度,手动调节保存后,它获得的亮度还是自动调节的亮度
    
   
    public static int getScreenBrightness(Activity activity) {   
     int nowBrightnessValue = 0;   
      
     ContentResolver resolver = activity.getContentResolver();   

     try{       

     nowBrightnessValue = android.provider.Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS); 

       }

      catch(Exception e) {      

      e.printStackTrace(); 

       }   

     return nowBrightnessValue;}//获取当前亮度
    
    //设置当前屏幕的亮度
   
    public static void setBrightness(Activity activity, int brightness) {  

//       Settings.System.putInt(activity.getContentResolver(),   
//
//      Settings.System.SCREEN_BRIGHTNESS_MODE,   
//
//      Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);   

     WindowManager.LayoutParams lp = activity.getWindow().getAttributes();  

      lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);  

      activity.getWindow().setAttributes(lp); }//设置亮度
    
//关闭屏幕亮度自动调节   
    public static void stopAutoBrightness(Activity activity) {  
    
     Settings.System.putInt(activity.getContentResolver(),         

        Settings.System.SCREEN_BRIGHTNESS_MODE,          

        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);}
       

 //停用自动调节亮度
    
    
//开启屏幕亮度自动调节 
    public static void startAutoBrightness(Activity activity) {  

      Settings.System.putInt(activity.getContentResolver(),          

      Settings.System.SCREEN_BRIGHTNESS_MODE,           

     Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);

     }    //开启亮度自动调节

 

//保存屏幕的亮度

    public static void saveBrightness(ContentResolver resolver, int brightness) {   

     Uri uri = android.provider.Settings.System.getUriFor("screen_brighness");  

      android.provider.Settings.System.putInt(resolver, "screen_brightness", brightness);   

     // resolver.registerContentObserver(uri, true, myContentObserver);  

      resolver.notifyChange(uri, null);}//保存亮度调节

注:1.设置屏幕亮度时应该先关闭自动调节功能

      2.保存屏幕亮度时清单注意

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ntc.what"
    android:versionCode="1"
   
   

    android:versionName="1.0" >

    
 <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
   
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".WhatActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

主Activity 的调用

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class WhatActivity extends Activity {
    private boolean ff=false;
 private Button btn;
 private SeekBar sb;
 private CheckBox cb;
 private View v1;
    private AlertDialog alt;
    private int m;
  
  
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        btn=(Button)this.findViewById(R.id.btn);
         ff=this.getSharedPreferences("hoperun", MODE_PRIVATE ).getBoolean("checkable", true);
         m=this.getSharedPreferences("hoperun", MODE_PRIVATE ).getInt("light",  getScreenBrightness(this));
         Toast.makeText(this, m+"", 10000).show();
         v1=this.mm(this);
    
 
        
         btn.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
    
     WhatActivity.this.alt=new AlertDialog.Builder(WhatActivity.this).setView(v1)
     .setTitle("test").setIcon(R.drawable.ic_launcher ).setMessage("目前亮度为"+m).setPositiveButton("ddddd", new DialogInterface.OnClickListener() {
      
      @Override
      public void onClick(DialogInterface dialog, int which) {
         Editor et=WhatActivity.this.getSharedPreferences("hoperun", MODE_PRIVATE ).edit();
        if(ff)
        et.putBoolean("checkable", ff);
        else{
         et.putBoolean("checkable", ff);
        
          Toast.makeText(WhatActivity.this, m+"", 10000).show();
         et.putInt("light", m);
        }

 
              et.commit();
       
      }
     }).show();}
    
        
             
        
         });
        
         handle();
     
        
    }

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

private View mm(WhatActivity whatActivity) {
      v1=LayoutInflater.from(this).inflate(R.layout.tt, null);
      sb=(SeekBar)v1.findViewById(R.id.seek);
         cb=(CheckBox)v1.findViewById(R.id.cb);
           return v1;
 }

////////////////////////////////////////////////////////////////////

private void handle(){
   
   
    if(ff)
       {  if( !isAutoBrightness(this.getContentResolver()))
        startAutoBrightness(this);
    
    
     cb.setChecked(true);
          sb.setVisibility(sb.GONE);
      //   alt.setMessage("目前亮度为:"+ getScreenBrightness(this));
        
           m=   getScreenBrightness(this);          
        
        this.setBrightness(this, m);
         
       }
       else{
        
        if(isAutoBrightness(this.getContentResolver()))
   {stopAutoBrightness(this);}
        cb.setChecked(false);
        sb.setVisibility(sb.VISIBLE);
        
          sb.setProgress(m);
        //  alt.setMessage("目前亮度为:"+m);
         
          sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
      boolean fromUser) {
     m=progress;
      setBrightness(WhatActivity.this, m);
     alt.setMessage("目前亮度为"+m);
     Toast.makeText(WhatActivity.this,m+"", 10000).show();
     
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
     m=seekBar.getProgress();
      setBrightness(WhatActivity.this, m);
     alt.setMessage("目前亮度为"+m);
     Toast.makeText(WhatActivity.this, m+"", 10000).show(); 
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
     
     m=seekBar.getProgress();
      setBrightness(WhatActivity.this, m);
     alt.setMessage("目前亮度为"+m);
     Toast.makeText(WhatActivity.this, m+"", 10000).show();
    }});
         
         
         
         
         
         
        
   }
       
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {
    
   
   
   if(cb.isChecked()){
     if( !isAutoBrightness(WhatActivity.this.getContentResolver()))
           WhatActivity.startAutoBrightness(WhatActivity.this);
       
       
        cb.setChecked(true);
             sb.setVisibility(sb.GONE);
            alt.setMessage("目前亮度为:"+ WhatActivity.getScreenBrightness(WhatActivity.this));
    ff=true;
    
    
   }else{ff=false;
    if(isAutoBrightness(WhatActivity.this.getContentResolver()))
    {WhatActivity.stopAutoBrightness(WhatActivity.this);}
         cb.setChecked(false);
         sb.setVisibility(sb.VISIBLE);
          m=WhatActivity.this.getSharedPreferences("hoperun", MODE_PRIVATE ).getInt("light",  WhatActivity.getScreenBrightness(WhatActivity.this));
           sb.setProgress(m);
           alt.setMessage("目前亮度为:"+m);
          
           sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

     @Override
     public void onProgressChanged(SeekBar seekBar, int progress,
       boolean fromUser) {
      m=progress;
       setBrightness(WhatActivity.this, m);
      alt.setMessage("目前亮度为"+m);
      Toast.makeText(WhatActivity.this, getScreenBrightness(WhatActivity.this)+"", 10000).show();
      
     }

     @Override
     public void onStartTrackingTouch(SeekBar seekBar) {
      m=seekBar.getProgress();
       setBrightness(WhatActivity.this, m);
      alt.setMessage("目前亮度为"+m);
      
     }

     @Override
     public void onStopTrackingTouch(SeekBar seekBar) {
      
      m=seekBar.getProgress();
       setBrightness(WhatActivity.this, m);
      alt.setMessage("目前亮度为"+m);
       Toast.makeText(WhatActivity.this, getScreenBrightness(WhatActivity.this)+"", 10000).show();
     }});
    
    
    
   } }}); 
    
   }

 

 

 

 win7截图按键盘Prt Scr SysRq复制到附件中的图片粘贴皆可(键盘右上角)

 

阅读更多
换一批

没有更多推荐了,返回首页