SharedPreferences用于保存登录数据

时间:2016-08-07 19:12:55

标签: android sharedpreferences

在我的应用程序中,用户启动应用程序并尝试登录,应用程序检查共享集中是否存在<使用者名称>使用所有用户的凭据列表,如果它不存在,请从头开始创建....这是我的问题是如何检查此Set的共享存在<使用者>

3 个答案:

答案 0 :(得分:1)

在这里,查看我的共享首选项代码。此代码将保存您的登录数据。

public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
TextView name;
TextView email;
public static final String mypreference = "mypref";
public static final String Name = "nameKey";
public static final String Email = "emailKey";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name = (TextView) findViewById(R.id.etName);
    email = (TextView) findViewById(R.id.etEmail);
    sharedpreferences = getSharedPreferences(mypreference,
            Context.MODE_PRIVATE);
    if (sharedpreferences.contains(Name)) {
        name.setText(sharedpreferences.getString(Name, ""));
    }
    if (sharedpreferences.contains(Email)) {
        email.setText(sharedpreferences.getString(Email, ""));

    }

}

public void Save(View view) {
    String n = name.getText().toString();
    String e = email.getText().toString();
    Editor editor = sharedpreferences.edit();
    editor.putString(Name, n);
    editor.putString(Email, e);
    editor.commit();
}

public void clear(View view) {
    name = (TextView) findViewById(R.id.etName);
    email = (TextView) findViewById(R.id.etEmail);
    name.setText("");
    email.setText("");

}

public void Get(View view) {
    name = (TextView) findViewById(R.id.etName);
    email = (TextView) findViewById(R.id.etEmail);
    sharedpreferences = getSharedPreferences(mypreference,
            Context.MODE_PRIVATE);

    if (sharedpreferences.contains(Name)) {
        name.setText(sharedpreferences.getString(Name, ""));
    }
    if (sharedpreferences.contains(Email)) {
        email.setText(sharedpreferences.getString(Email, ""));

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

答案 1 :(得分:0)

这是一个简单的登录代码,我们可以通过编辑类的putString方法存储数据

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et1 = (EditText) findViewById(R.id.editText);
    et2 = (EditText) findViewById(R.id.editText2);
    btn = (Button) findViewById(R.id.button);
    btn3 = (Button) findViewById(R.id.button3);
    btn3 = (Button) findViewById(R.id.button3);
    ct = (Button) findViewById(R.id.ct);
    final SQLiteDatabase db = openOrCreateDatabase("DemoDb",MODE_ENABLE_WRITE_AHEAD_LOGGING,null);
    ct.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           db.execSQL("create table login(LoginId varchar(10) primary key,Password varchar(10),Name varchar(10));");
        }
    });
    sp = getSharedPreferences("myLogin", MODE_PRIVATE);
    if(!sp.getBoolean("LogInMode",false)) {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( et1.getText().toString().length()==0 || et2.getText().toString().length()==0){
                    Toast.makeText(getBaseContext(), "User Not Found", Toast.LENGTH_SHORT).show();
                }else {
                    String data = "content://com.example.maity.dbdemo.123/DemoDb";
                    Uri uri = Uri.parse(data);
                    ContentResolver resolver = getContentResolver();
                    String[] ar = {"", ""};
                    ar[0] = et1.getText().toString().trim();
                    ar[1] = et2.getText().toString().trim();
                    final Cursor c = resolver.query(uri, null, null, ar, null);
                    if (c.moveToNext()) {
                        if ((et1.getText().toString().trim().equals(c.getString(c.getColumnIndex("LoginId")).toString())) &&
                                (et2.getText().toString().trim().equals(c.getString(c.getColumnIndex("Password")).toString()))) {
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putBoolean("LogInMode", true);
                            editor.putString("User", c.getString(c.getColumnIndex("Name")).toString());
                            editor.commit();
                            Intent intent = new Intent(MainActivity.this, WelcomePage.class);
                            startActivity(intent);
                            finish();
                        }
                    }else {
                        Toast.makeText(getBaseContext(), "User Not Found", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
    else{
        Intent intent = new Intent(MainActivity.this, WelcomePage.class);
        startActivity(intent);
        finish();
    }
}

完整代码

int main(int argc, const char * argv[]) {
    const int length = 19;
        int data[length] = {1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20};
        Node* root = new Node(12);

        for(int i = 0;i<length;i++){
            insert(data[i], &root);
        }
return 0;
}

答案 2 :(得分:0)

如果您的登录api响应如下所示

{
  "status": true,
  "message": "Login Success",
  "data": {
    "user_id": "1",
    "first_name": "Ketan",
    "last_name": "Ramani",
    "username": "ketanramani"
  }
}

然后,您可以使用以下代码动态保存所有登录响应

SharedPreferences preferences = getApplicationContext().getSharedPreferences("LoginPref", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();

JSONObject jsonObject = null;
try {
    jsonObject = new JSONObject(dataObject.toString());
    Iterator<String> iterator = jsonObject.keys();

    while (iterator.hasNext()) {
        String key = iterator.next();
        editor.putString(key, jsonObject.optString(key)).apply();
    }
} catch (JSONException e) {
    e.printStackTrace();
}

您的数据将保存在如下所示的共享首选项中

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="user_id">1</string>
    <string name="first_name">Ketan</string>
    <string name="last_name">Ramani</string>
    <string name="username">ketanramani</string>
</map>