lundi 2 mai 2016

Issues in Shared Prfernces and Sqlite database

My issue is Data is sucessfully inserted in to database but shared prefernces is not working when i m register after i m destroy the app they come from starting splashscreen.my intention is like facebook app when i m once login and after killing app again reopen it never ask login page it dispaly home page.i want like that.can any one tell how to combined sharedprefernces with database? sorry for poor english.pls understand public class MainActivity extends Activity {

EditText et,et1,et2,et3,et4,et5;

TextView tv;
Button btn;
static MyDatabase mydb;
SQLiteDatabase db;
public static final String Filename = "logindata";
public static final String key = "status";
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.text);
    btn = (Button)findViewById(R.id.submit);
    et = (EditText) findViewById(R.id.first);
    et1 = (EditText) findViewById(R.id.last);
    et2 = (EditText) findViewById(R.id.email);
    sp = getSharedPreferences(Filename,MODE_PRIVATE);
    boolean res = sp.getBoolean(key,false);
    if (res)
    {
        Intent i = new Intent(MainActivity.this,Result.class);
        startActivity(i);
        Toast.makeText(MainActivity.this, "sp created", Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(MainActivity.this, "temp", Toast.LENGTH_SHORT).show();
    }
    mydb = new MyDatabase(this);
    db = mydb.getWritableDatabase();
    ConnectivityManager connect = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo net = connect.getActiveNetworkInfo();
    if (net!=null&&net.isConnected())
    {
        tv.setVisibility(View.INVISIBLE);
    }
    else
    {
        btn.setEnabled(false);
    }
}
public void submitDetails(View v) {
   String user = et.getText().toString();
    String pwd = et1.getText().toString();
    String email = et2.getText().toString();
 String emailpatern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    if (user.equals("")) {
        et.setError("pls enter name");
    } else if (pwd.equals("")) {
        et1.setError("pls enter last name");
    } else if (email.equals("")) {
        et2.setError("Pls Enter Valid Email");
    } else if (!email.matches(emailpatern)) {
        et2.setError("Pls Enter Valid email charcters");
    }
    else if (user.equals(mydb))
    {
        SharedPreferences.Editor ed = sp.edit();
        ed.putBoolean(key, true);
        ed.commit();
        Intent i = new Intent(MainActivity.this,Result.class);
        startActivity(i);
    }
    else {
        Intent i = new Intent(MainActivity.this, Result.class);
        i.putExtra("k1", user);
        i.putExtra("k2", pwd);
        i.putExtra("k3", email);
        startActivity(i);
    }
    String qry = "insert into register values('"+user+"','"+pwd+"','"+email+"')";
    try
    {
     db.execSQL(qry);
        Toast.makeText(MainActivity.this, "registered", Toast.LENGTH_SHORT).show();
    }catch (Exception e)
    {
        Log.e("TAG:InsertionException",""+e);
    }

Aucun commentaire:

Enregistrer un commentaire