【发布时间】:2018-02-08 13:26:36
【问题描述】:
我的应用程序有一个 Switch。它用于打开覆盖服务。它工作正常,但问题在于开关的状态。关闭应用程序并重新启动应用程序后,它的状态发生了变化。它总是显示为关闭。
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
private WindowManager windowManager;
private BillingClient mBillingClient;
Switch aSwitch;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.essential);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
aSwitch = (Switch) findViewById(R.id.switch_ph1);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked == true) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(getApplicationContext())) {
start();
} else {
Toast.makeText(getApplicationContext(), "Please permit drawing over apps.", Toast.LENGTH_SHORT).show();
checkDrawOverlayPermission();
}
} else {
start();
}
} else {
stop();
}
}
});
【问题讨论】:
-
查找共享首选项
-
您可以检查您的服务是否正在运行,而不是保存按钮状态,然后使用该状态来检查/取消选中您的开关按钮
标签: android android-layout button sharedpreferences android-togglebutton