我有一个应用程序,其中亮度保存在配置文件中。连接选定的wifi时,配置文件处于活动我用shareprefrences
保存亮度设置。当我在seekbar
显示的值中保存并连接到wifi时,我设置的相同但亮度不会增加或减少我在设置中设置的值。亮度增加或减少只有当我用seekbar
手动更改时。请告诉我我在哪里错误的是活动代码。
public class Profile1Activity extends Activity {
//TextViews to show details of volume and brightness
private TextView tVBrightness, tVVolume;
//SeekBars to set volume and brightness
private SeekBar sbVolume, sbBrightness = null;
//Variable to store brightness value
private int brightness = 1;
//Content resolver used as a handle to the system's settings
private ContentResolver cResolver;
//Window object, that will store a reference to the current window
private Window window;
EditText e1;
Button b1;
public static final String MyPREFERENCESSS = "MyPrefsss";
public static final String HOMEWIFI = "homewifi";
Context context = this;
SharedPreferences sharedpreferences;
public static final String BRIGHTNESSS = "brightnessProfile1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile1);
sbBrightness = (SeekBar) findViewById(R.id.sbBrightness1);
tVBrightness = (TextView) findViewById(R.id.tVBrightness);
e1 = (EditText) findViewById(R.id.ed2);
b1 = (Button) findViewById(R.id.but2);
sharedpreferences = getSharedPreferences(MyPREFERENCESSS, Context.MODE_PRIVATE);
final String homewifi = sharedpreferences.getString(HOMEWIFI, "");
e1.setText(homewifi);
sbBrightness.setProgress(sharedpreferences.getInt(BRIGHTNESSS, 0));
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String homewifi = e1.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(HOMEWIFI, homewifi);
//Suhas
editor.putInt(BRIGHTNESSS, sbBrightness.getProgress());
editor.commit();
Toast.makeText(Profile1Activity.this, "Thanks", Toast.LENGTH_SHORT).show();
}
});
////////////////////////////////////////////////////////////////////////////////////
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo conn = wifiManager.getConnectionInfo();
//Toast.makeText(MainActivity.this, con.getSSID()+"",Toast.LENGTH_LONG).show();
if (conn.getSSID().toString().equalsIgnoreCase("\"" + homewifi + "\"")) {
initializeControls1();
sbBrightness.setProgress(sharedpreferences.getInt(BRIGHTNESSS, 0));
}
}
handler.postDelayed(this, 100);
}
}, 100);
}
private void initializeControls1() {
//Get the content resolver
cResolver = getContentResolver();
//Get the current window
window = getWindow();
//Set the seekbar range between 0 and 255
sbBrightness.setMax(255);
//Set the seek bar progress to 1
sbBrightness.setKeyProgressIncrement(1);
try
{
//Get the current system brightness
brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException e)
{
//Throw an error case it couldn't be retrieved
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
//Set the progress of the seek bar based on the system's brightness
sbBrightness.setProgress(brightness);
//Register OnSeekBarChangeListener, so it can actually change values
sbBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar seekBar)
{
//Set the system brightness using the brightness variable value
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
//Get the current window attributes
LayoutParams layoutpars = window.getAttributes();
//Set the brightness of this window
layoutpars.screenBrightness = brightness / (float)255;
//Apply attribute changes to this window
window.setAttributes(layoutpars);
run1();
}
public void onStartTrackingTouch(SeekBar seekBar)
{
//Nothing handled here
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
//Set the minimal brightness level
//if seek bar is 20 or any value below
if(progress<=20)
{
//Set the brightness to 20
brightness=20;
}
else //brightness is greater than 20
{
//Set brightness variable based on the progress bar
brightness = progress;
}
//Calculate the brightness percentage
float perc = (brightness /(float)255)*100;
//Set the brightness percentage
tVBrightness.setText("Brightness: "+(int)perc +" %");
}
});
}
public void run1(){
String homewifi = e1.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(HOMEWIFI, homewifi);
//Suhas
editor.putInt(BRIGHTNESSS, sbBrightness.getProgress());
editor.commit();
// Toast.makeText(Profile1Activity.this, "Thanks", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
在prefrance
中设置搜索条的值时也设置亮度答案 1 :(得分:0)
添加此代码...当您的个人资料片段或活动加载时,只需要这么多代码来增加和设置手机亮度的默认值为seekbar:
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setMax(255);
float curBrightnessValue = 0;
try {
curBrightnessValue = android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
int screen_brightness = (int) curBrightnessValue;
seekBar.setProgress(screen_brightness);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
progress = progresValue;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do something here,
// if you want to do anything at the start of
// touching the seekbar
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
progress);
}
});
}