Android:与共享偏好保持联系

时间:2015-07-07 14:45:20

标签: java android login sharedpreferences logout

您好我做了一个包含多个活动的Android应用程序,对于登录注销阶段,我使用了此链接的示例:

http://www.tutorialspoint.com/android/android_session_management.htm

我想在登录阶段添加一个复选框,当点击它时,应用程序也会记住用户退出时没有注销, 但是当没有单击此复选框时,用户可以访问多个活动,但是当他退出而没有注销时,仍然会注销。 出于这个原因,我在这种模式下更改了MainActivity:

 public class MainActivity extends Activity {

     private EditText username,password;
    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String name = "nameKey"; 
   public static final String pass = "passwordKey"; 

    SharedPreferences sharedpreferences;
    private CheckBox check;



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


    username = (EditText)findViewById(R.id.editText1);
     password = (EditText)findViewById(R.id.editText2);
    username.setText("admin");
    password.setText("admin");
    check   = (CheckBox) findViewById(R.id.checkBox1);
        }

    @Override
    protected void onResume() {

    sharedpreferences=getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
        if (sharedpreferences.contains(name))
            {
            if(sharedpreferences.contains(pass)){
                    if (check.isChecked()) {

                    Intent i = new Intent(this,com.example.sessionmanagement.
    Welcome.class);

                startActivity(i);
                    }

                else
                    {

        logout();
                }
            }

            }
        super.onResume();
        }
 public void login(View view){

            Editor editor = sharedpreferences.edit();
    String u = username.getText().toString();
        String p = password.getText().toString();

            if(u.equals("admin") && p.equals("admin")){

            editor.putString(name, u);

            editor.putString(pass, p);

            editor.commit();

            Intent i = new Intent(this,com.example.sessionmanagement.Welcome.class);

            startActivity(i);

            }

    else{

    Toast.makeText(MainActivity.this, "ko", Toast.LENGTH_SHORT).show();


        }
        }


    @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;

        }

      public void onPause() {
   super.onPause();
    this.finish();
        }



    public void closing() {

        finish();
        }



    public void logout(){

    SharedPreferences sharedpreferences = getSharedPreferences

    (MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);

    Editor editor = sharedpreferences.edit();


    editor.clear();

    editor.commit();

    moveTaskToBack(true);

    this.finish();
        }


    }

并在file.xml中

<CheckBox
       android:id="@+id/checkBox1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignLeft="@+id/textView1"
       android:layout_centerVertical="true"
       android:checked="true"
       android:onClick="onResume"
       android:text="CheckBox" />

此代码不起作用,因为未选中复选框时会直接注销,用户无法访问其他活动。如果未选中复选框,如何编写条件? 我该如何解决这个问题?

0 个答案:

没有答案