我正在使用共享首选项来存储数据。每当我关闭并打开应用程序时,它将读取共享的prefs值并保持相同的状态。当我按下时我正确。但是当我从内存中删除应用程序时或强制关闭,共享首选项得到清除。当我重新打开app.how以获取值时,即使我从内存中删除了应用程序,我也得到了空值。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Conveyer=(SwitchCompat) findViewById(R.id.tubelight);
fan=(SwitchCompat) findViewById(R.id.Fan);
ac=(SwitchCompat) findViewById(R.id.AC);
motor=(SwitchCompat) findViewById(R.id.Motor);
sharedPreferences = getApplicationContext().getSharedPreferences("MyPrefer", MODE_PRIVATE);
editor = sharedPreferences.edit();
A=sharedPreferences.getString("Conveyer","0");
B=sharedPreferences.getString("fan","0");
C=sharedPreferences.getString("ac","0");
D=sharedPreferences.getString("motor","0");
new fetchingjson().execute();
if(A=="1"){
Conveyer.setChecked(true);
}else{
Conveyer.setChecked(false);
}
if(B=="1"){
fan.setChecked(true);
}else{
fan.setChecked(false);
}
if(C=="1"){
ac.setChecked(true);
}else{
ac.setChecked(false);
}
if(D=="1"){
motor.setChecked(true);
}else{
motor.setChecked(false);
}
Conveyer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {
if(ischecked){
A="1";
Toast.makeText(MainActivity.this, "conveyer is turned on", Toast.LENGTH_SHORT).show();
}else{
A="0";
Toast.makeText(MainActivity.this, "conveyer is turned off", Toast.LENGTH_SHORT).show();
}
new fetchingjson().execute();
}
});
fan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {
if(ischecked){
B="1";
Toast.makeText(MainActivity.this, "Fan is turned on", Toast.LENGTH_SHORT).show();
}else{
B="0";
Toast.makeText(MainActivity.this, "Fan is turned off", Toast.LENGTH_SHORT).show();
}
new fetchingjson().execute();
}
});
ac.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {
if(ischecked){
C="1";
Toast.makeText(MainActivity.this, "AC is turned on", Toast.LENGTH_SHORT).show();
}else{
C="0";
Toast.makeText(MainActivity.this, "AC is turned off", Toast.LENGTH_SHORT).show();
}
new fetchingjson().execute();
}
});
motor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {
if(ischecked){
D="1";
Toast.makeText(MainActivity.this, "Motor is turned on", Toast.LENGTH_SHORT).show();
}else{
D="0";
Toast.makeText(MainActivity.this, "Motor is turned off", Toast.LENGTH_SHORT).show();
}
new fetchingjson().execute();
}
});
}
private class fetchingjson extends AsyncTask<String,String,String>{
String url = "http://youwalks.com/get.php?a="+A+"&b="+B+"&c="+C+"&d="+D;
ProgressDialog dialogProgress = new ProgressDialog(MainActivity.this);
AndroidHttpClient mmClient = AndroidHttpClient.newInstance("");
private String response="";
private String jstr;
char phase[];
@Override
protected String doInBackground(String... strings) {
HttpGet request = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
response = mmClient.execute(request, responseHandler);
} catch (ClientProtocolException exception) {
exception.printStackTrace();
return null;
} catch (IOException exception) {
exception.printStackTrace();
return null;
}
jstr=""+response;
if(jstr.equals(null))
Log.i("url", "" + url);
Log.i("routes", "" + response);
return null;
}
@Override
protected void onPreExecute() {
dialogProgress.setCancelable(true);
dialogProgress.setMessage("Please wait..");
dialogProgress.setIndeterminate(false);
dialogProgress.show();
}
@Override
protected void onPostExecute(String s) {
dialogProgress.dismiss();
Log.i("responseeer",""+A);
}
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
if(fan.isChecked()){
editor.putString("fan", "1");
editor.commit();
Log.i("editor","true");
}
else{
editor.putString("fan", "0");
editor.commit();
Log.i("editor","false");
}
if(motor.isChecked()){
editor.putString("motor", "1");
editor.commit();
Log.i("editorac","true");
}
else{
editor.putString("motor", "0");
editor.commit();
Log.i("editorac","false");
}
if(Conveyer.isChecked()){
editor.putString("Conveyer", "1");
editor.commit();
Log.i("editorac","true");
}
else{
editor.putString("Conveyer", "0");
editor.commit();
Log.i("editorac","false");
}if(ac.isChecked()){
editor.putString("ac", "1");
editor.commit();
Log.i("editorac","true");
}
else{
editor.putString("ac", "0");
editor.commit();
Log.i("editorac","false");
}
}
`