我有一个应用程序,通过它我可以手动更改设备的亮度。我想使用传感器来做到这一点。我必须添加什么代码?另外我必须在清单中进行哪些更改?
这是我的代码:
package com.example.brightnessdemo;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings.System;
import android.app.Activity;
import android.content.ContentResolver;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
private SeekBar brightbar;
private int brightness;
private ContentResolver cResolver;
private Window window;
TextView tv;
//float BackLightValue = 0.5f;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
brightbar=(SeekBar)findViewById(R.id.seekBar1);
tv=(TextView)findViewById(R.id.textView1);
cResolver=getContentResolver();
window=getWindow();
brightbar.setMax(100);
brightbar.setKeyProgressIncrement(1);
try
{
brightness=System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
brightbar.setProgress(brightness);
int SysBackLightValue = (int)(brightness);
android.provider.Settings.System.putInt(getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS,SysBackLightValue);
brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
android.view.WindowManager.LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness;
window.setAttributes(layoutpars);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
brightness = progress;
tv.setText(""+brightness);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}