2016-09-22 37 views
0

编辑维护用户会话中的android应用程序。但问题是,当用户关闭手机并再次开机时,对于他们来说,它再次显示登录屏幕。我不知道该怎么做,请帮忙。下面是我对mainActivity代码:使用共享prference

public class main extends AppCompatActivity { 
public Button submit; 


public static final String MyPREFERENCES = "MyPrefs" ; 
public static final String email = "emailkey"; 
SharedPreferences sharedpreferences; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity1); 

submit = (Button) findViewById(R.id.btn_login); 

ImageView i1 = (ImageView) findViewById(R.id.imgLogo); 
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb); 
String checkBoxText = "I agree to all the "; 
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox); 
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
checkBox.setText(Html.fromHtml(checkBoxText)); 
checkBox.setMovementMethod(LinkMovementMethod.getInstance()); 

submit.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     final EditText e1 = (EditText) findViewById(R.id.input_email); 
     final EditText p1 = (EditText) findViewById(R.id.input_password); 
     final String e = e1.getText().toString(); 
     final String password = p1.getText().toString(); 

     SharedPreferences.Editor editor = sharedpreferences.edit(); 

     editor.putString(email, e); 

     editor.commit(); 

和代码次活动:

 else if(position==3) 
     { 
      AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
      builder.setMessage("Are you sure you want to Logout?") 
        .setCancelable(false) 
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 

          SharedPreferences sharedpreferences = getSharedPreferences(main.MyPREFERENCES, Context.MODE_PRIVATE); 
          SharedPreferences.Editor editor = sharedpreferences.edit(); 
          editor.clear(); 
          editor.commit(); 
          Intent i = new Intent(getApplicationContext(), main.class); 
          // Closing all the Activities 
          i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

          // Add new Flag to start new Activity 
          i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

          // Staring Login Activity 
          getApplicationContext().startActivity(i); 


          finish(); 
         } 
        }) 
        .setNegativeButton("No", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 

     } 

在闪屏活动,我cheking的条件,但每当手机关掉它现在显示的登录屏幕甚至没有启动画面。

 public class Splash_Screen extends FragmentActivity{ 
private static int SPLASH_TIME_OUT = 1000; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.splash_screen); 
    SharedPreferences sharedpreferences = getSharedPreferences(main.MyPREFERENCES, Context.MODE_PRIVATE); 

    if (sharedpreferences.contains("email")) { 
     Intent i = new Intent(Splash_Screen.this, MainActivity.class); 
     startActivity(i); 
    } 
    else{ 
     Intent i = new Intent(Splash_Screen.this,Splash_Screen.class); 
     startActivity(i); 
     //get yourkey value and load screen 
    } 
    new Handler().postDelayed(new Runnable() { 

    /* 
     * Showing splash screen with a timer. This will be useful when you 
     * want to show case your app logo/company 
     */ 

     @Override 
     public void run() { 
      // This method will be executed once the timer is over 
      // Start your app main activity 
      Intent i = new Intent(Splash_Screen.this, main.class); 
      startActivity(i); 

      // close this activity 
      finish(); 
     } 
    }, SPLASH_TIME_OUT); 
} 
} 
+0

存储您的登录信息。在喜欢例如:isLoggedIn =真,因此在应用程序的开始检查条件,并显示相应的屏幕 –

回答

0

检查共享的前提是否存在或在您的启动画面或启动器屏幕上。

if (sharedpreferences.contains("yourkey")) { 
     //get yourkey value and load screen 
      } 

借助此执行操作显示主屏幕或登录页面。

 public class Splash_Screen extends FragmentActivity{ 
     private static int SPLASH_TIME_OUT = 1000; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
requestWindowFeature(Window.FEATURE_NO_TITLE); 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     WindowManager.LayoutParams.FLAG_FULLSCREEN); 
setContentView(R.layout.splash_screen); 
SharedPreferences sharedpreferences = getSharedPreferences(main.MyPREFERENCES, Context.MODE_PRIVATE); 

    //change here 
if (sharedpreferences.contains(main.email)) { 
    Intent i = new Intent(Splash_Screen.this, MainActivity.class); 
    startActivity(i); 
} 
else{ 

从别的

// Intent i = new Intent(Splash_Screen.this,Splash_Screen.class); 
    // startActivity(i); 
    //get yourkey value and load screen 
} 
new Handler().postDelayed(new Runnable() { 

/* 
    * Showing splash screen with a timer. This will be useful when you 
    * want to show case your app logo/company 
    */ 

    @Override 
    public void run() { 
     // This method will be executed once the timer is over 
     // Start your app main activity 
     Intent i = new Intent(Splash_Screen.this, main.class); 
     startActivity(i); 
+0

看到我编辑的帖子,它不是未来 – user6798766

+0

什么是main.MyPREFERENCES请给我代码 – anddevmanu

+0

编辑...你可以看到那代码 – user6798766

0

在登录删除此代码的OnCreate()

 SharedPreferences prefs; 
      String settingsTAG = "AppNameSettings"; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity1); 
      prefs = getSharedPreferences(settingsTAG, 0); 
      if(checkLoginStatus()){ 
       callIntent(); 
      } 
      submit = (Button) findViewById(R.id.btn_login); 

      ImageView i1 = (ImageView) findViewById(R.id.imgLogo); 
      final ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb); 
      String checkBoxText = "I agree to all the "; 
      final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox); 

      checkBox.setText(Html.fromHtml(checkBoxText)); 
      checkBox.setMovementMethod(LinkMovementMethod.getInstance()); 

      submit.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        final EditText e1 = (EditText) findViewById(R.id.input_email); 
        final EditText p1 = (EditText) findViewById(R.id.input_password); 
        final String e = e1.getText().toString(); 
        final String password = p1.getText().toString(); 

        SharedPreferences.Editor editor = prefs.edit(); 

        editor.putBoolean("rb0", true); 

        editor.commit(); 

       } 
} 

       private boolean checkLoginStatus(){ 


        boolean rb0 = prefs.getBoolean("rb0", false); 
        return rb0; 

       }