如何保存在android的开关(按钮)的状态 [英] how to save the state of switch(button) in android
问题描述
我使用开关
(如Android 切换按钮
),而不是在我的Android应用程序的正常按钮。在code,同时启用和禁用开关正常工作。但我想存储开关的状态。假设我使开关,并关闭我的应用程序在后台code将会运行得很好,但开关状态将变更为禁用。
当我关闭开关状态变为禁用应用程序每次。有什么办法来存储开关状态?
mySwitch.setOnClickListener(新View.OnClickListener(){@覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
如果(mySwitch.isChecked()){
共享preferences.Editor编辑= getShared preferences(com.mobileapp.smartapplocker
MODE_PRIVATE).edit();
editor.putBoolean(服务开,真正的);
editor.commit();
} 其他{
共享preferences.Editor编辑= getShared preferences(com.mobileapp.smartapplocker
MODE_PRIVATE).edit();
editor.putBoolean(服务关,FALSE);
editor.commit();
}
}
}
我觉得你是在$ pferences如何在Android共享$ P工作的困惑。他们基本上是键值对。因此,为了检索特定值,密钥必须是相同的。
下面给你一个例子:
mySwitch.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
共享preferences.Editor编辑= getShared preferences(com.mobileapp.smartapplocker,MODE_PRIVATE).edit();
editor.putBoolean(SERVICE_STATUS,mySwitch.isChecked());
editor.commit();
}
}
现在在任何你是检查服务
共享preferences preFS = getShared preferences(com.mobileapp.smartapplocker,MODE_PRIVATE);
布尔switchState = pref.getBoolean(SERVICE_STATUS,FALSE); 如果(switchState){
//不要你的服务工作,在选择上
}其他{
//服务code关闭
}
希望帮助
I am using switch
(like android togglebutton
) instead of normal buttons in my android app. The code works fine while enabling and disabling switches. But i want to store the state of the switch. Suppose i enable the switch and close my application the background code will run fine but the switch state will change to disabled.
Every time when i close the application the switch state becomes disabled. Is there any way to store the switch State?
mySwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mySwitch.isChecked()) {
SharedPreferences.Editor editor = getSharedPreferences ("com.mobileapp.smartapplocker",
MODE_PRIVATE).edit();
editor.putBoolean("Service On", true);
editor.commit();
}
else {
SharedPreferences.Editor editor = getSharedPreferences ("com.mobileapp.smartapplocker",
MODE_PRIVATE).edit();
editor.putBoolean("Service Off", false);
editor.commit();
}
}
}
I think you are confused on how shared preferences work in android. They are basically key value pairs. So in order to retrieve a particular value, the key has to be same.
Giving you an example below:
mySwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences.Editor editor = getSharedPreferences("com.mobileapp.smartapplocker", MODE_PRIVATE).edit();
editor.putBoolean("service_status", mySwitch.isChecked());
editor.commit();
}
}
Now where ever you are check for service
SharedPreferences prefs = getSharedPreferences("com.mobileapp.smartapplocker", MODE_PRIVATE);
boolean switchState = pref.getBoolean("service_status", false);
if(switchState){
//Do your work for service is selected on
} else {
//Code for service off
}
Hope that helps
这篇关于如何保存在android的开关(按钮)的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!