我最近在该项目中从事制作屏幕调光器应用程序的工作,但遇到一个问题。在我的应用程序中,有一个 MaterialAnimatedSwitch 可以正常工作,但是当应用程序转为反手时,该开关会自动关闭,但应用程序可以完美地继续工作。我希望无论用户何时回到应用程序界面,开关都保持活动状态或启用状态。我使用MaterialAnimatedSwitch来执行此任务。 我希望每当应用恢复时,开关就处于活动状态或打开。
public class MainActivity extends AppCompatActivity {
int b1 = 40;
int b2 = 90;
DiscreteSeekBar bSeekBar;
ChatHeadService binder=null;
boolean isOn = false;
boolean mBounded;
public boolean isRunning=false;
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
mBounded = false;
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBounded = true;
LocalBinder mLocalBinder = (LocalBinder) service;
mService = mLocalBinder.getServerInstance();
int brightness = mService.getBrightness();
int brightness2 = mService.getBrightness2();
seekBar.setProgress(brightness);
bSeekBar.setProgress(brightness2);
}
};
ChatHeadService mService;
RadioGroup radioGroup;
DiscreteSeekBar seekBar;
MaterialAnimatedSwitch aSwitch;
SharedPreferences sharedPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Kit[]{new Crashlytics()});
setContentView(R.layout.activity_main);
initInstances();
if (isOn && !isMyServiceRunning(ChatHeadService.class)) {
startService(new Intent(this, ChatHeadService.class));
}
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
for (RunningServiceInfo service : ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
private void initInstances() {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
seekBar = (DiscreteSeekBar) findViewById(R.id.seekBar);
bSeekBar = (DiscreteSeekBar) findViewById(R.id.brightnessSeekBar);
int b = sharedPrefs.getInt("b", 40);
int bb = sharedPrefs.getInt("bb", 90);
seekBar.setProgress(b);
bSeekBar.setProgress(bb);
aSwitch=(MaterialAnimatedSwitch)findViewById(R.id.switch1);
aSwitch.setClickable(sharedPrefs.getBoolean("isOn",false));
aSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(boolean b) {
if(b){
sharedPrefs.edit().putBoolean("isOn", true).apply();
isOn = true;
isRunning=aSwitch.isChecked();
startService(new Intent(MainActivity.this, ChatHeadService.class));
bindService();
}
else{
sharedPrefs.edit().putBoolean("isOn", false).apply();
isOn = false;
unBindService();
stopService(new Intent(MainActivity.this, ChatHeadService.class)); }
}
});
seekBar.setOnProgressChangeListener(new OnProgressChangeListener() {
public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
b1 = value;
if (mService != null) {
mService.setBrightness(value, b2);
}
sharedPrefs.edit().putInt("b", value).apply();
}
public void onStartTrackingTouch(DiscreteSeekBar seekBar) {
}
public void onStopTrackingTouch(DiscreteSeekBar seekBar) {
}
});
bSeekBar.setOnProgressChangeListener(new OnProgressChangeListener() {
public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
b2 = value;
if (mService != null) {
mService.setBrightness(b1, value);
}
sharedPrefs.edit().putInt("bb", value).apply();
}
public void onStartTrackingTouch(DiscreteSeekBar seekBar) {
}
public void onStopTrackingTouch(DiscreteSeekBar seekBar) {
}
});
}
@Override
protected void onStart() {
super.onStart();
if (isOn) {
bindService();
}
}
public void bindService() {
bindService(new Intent(this, ChatHeadService.class), mConnection,Context.BIND_AUTO_CREATE);
}
public void unBindService() {
if (mBounded) {
unbindService(mConnection);
mBounded = false;
}
}
@Override
protected void onPause() {
super.onPause();
isRunning=aSwitch.isPressed();
}
@Override
protected void onResume() {
super.onResume();
if(!isOn == aSwitch.isPressed()){
aSwitch.toggle();
}
}
}
谢谢!
This is the screenshot when app is just open
This is the screenshot when night mode is ON
But When the going to backhand and then open the switch state changed
答案 0 :(得分:1)
我建议将开关的值存储在您的全局变量之一中,然后恢复使用开关再次设置它,如下所示:
public boolean isRunning = false;
@Override
protected void onPause() {
super.onPause();
isRunning = switchAler.isChecked();
}
@Override
protected void onResume() {
super.onResume();
binding.switchAler.setChecked(isRunning);
if(!isRunning == switchAler.isChecked()){
switchAler.toggle();
}
}
答案 1 :(得分:0)
您要在“共享”首选项中保存开关状态,但不使用它。
只需在aswitch.setOnCheckedChangeListener
上方添加以下行
行,它将应用您在首选项中保存的状态
aSwitch.setChecked(sharedPrefs.getBoolean("isOn", false)); //false default