使用俩个SharedPreferences 存值,清理手机后台再次打开应用第一个SharedPreferences 中的值没有了

qq_28318901 2017-06-14 04:07:07
package com.a91zsc.www.myapplication.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.a91zsc.www.myapplication.R;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.os.SystemClock.sleep;

public class LoginActivity extends AppCompatActivity {

private EditText accountEdit;

private EditText passwordEdit;

private Button login;

private OkHttpClient client;

private Response response = null;
private String responseData = null;
private ProgressDialog progressDialog;
private Context context;
Handler mHandler = new Handler();
Intent intent = new Intent();
private int intDate = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences pref1 = getSharedPreferences("data", MODE_PRIVATE);
String account = pref1.getString("account", "");

if (!(account == null || account.length() <= 0)) {
intent = new Intent(LoginActivity.this, BluetoothActivity.class);
startActivity(intent);
finish();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getSupportActionBar().hide();
accountEdit = (EditText) findViewById(R.id.account);
passwordEdit = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("间隔俩秒");
final String account = accountEdit.getText().toString();
final String password = passwordEdit.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
try {
client = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
.add("account", account)
.add("password", password)
.build();
Request request = new Request.Builder()
.url("https://www.91zsc.com/Home/Print/login")
.post(requestBody)
.build();
response = client.newCall(request).execute();
responseData = response.body().string();
if (responseData.equals("oo")) {
Log.e("loginoo", responseData);
SharedPreferences.Editor editor1 = getSharedPreferences("data", MODE_PRIVATE).edit();
editor1.putString("acc", account);
editor1.putString("password", password);
editor1.apply();
Intent intent = new Intent(LoginActivity.this, BluetoothActivity.class);
startActivity(intent);
finish();
} else {
Log.e("loginxx", responseData);
}

} catch (Exception e) {
e.printStackTrace();

}
}
}).start();

}
}

);
}
}

工具类中的方法

public class toolsFileIO {
public static final String BLUEDRIVE = "blueDriveName";
public static final String MYFILEDATA = "userConfig";
SharedPreferences sharedPreferences;
FileOutputStream outputStream;

//从SharedPreferences获取BlueDrive数据
public String getBlueTooth(Context context) {
sharedPreferences = context.getSharedPreferences(BLUEDRIVE, MODE_PRIVATE);
String drive = sharedPreferences.getString("blueDrive", "");

return drive;
}

//存入Blue地址在从SharedPreferences获取数据
public void putBlueToothDrive(Context context, String string) {
sharedPreferences = context.getSharedPreferences(BLUEDRIVE, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("blueDrive", string);
editor.apply();
}
...全文
2388 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_28318901 2017-06-14
  • 打赏
  • 举报
回复
这是俩个不同sharedPreferences 中存的数据 我存用户名和密码的sharedPreferences 就不知道怎么回事 存不进去sharedPreferences

7,655

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧