Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓:与共享偏好保持联系_Java_Android_Login_Sharedpreferences_Logout - Fatal编程技术网

Java 安卓:与共享偏好保持联系

Java 安卓:与共享偏好保持联系,java,android,login,sharedpreferences,logout,Java,Android,Login,Sharedpreferences,Logout,嗨,我做了一个Android应用程序,有几个活动,在登录注销阶段,我使用了以下链接示例: 我想在登录阶段添加一个复选框,当点击应用程序时,记住用户在未注销的情况下退出, 但是,如果未单击此复选框,则用户可以访问多个活动,但如果用户退出而未注销,则仍然会注销。 因此,我在此模式下更改了MainActivity: public class MainActivity extends Activity { private EditText username,password; p

嗨,我做了一个Android应用程序,有几个活动,在登录注销阶段,我使用了以下链接示例:

我想在登录阶段添加一个复选框,当点击应用程序时,记住用户在未注销的情况下退出, 但是,如果未单击此复选框,则用户可以访问多个活动,但如果用户退出而未注销,则仍然会注销。 因此,我在此模式下更改了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" />

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

为什么要在单击事件上调用
onResume()
?因为我希望在应用程序的OnPause之后保持连接,因此我在onResume()中进行了控制。我错了吗?我不理解你的逻辑。我希望通过单击复选框,用户即使在onPause应用程序之后仍保持登录状态,而如果未单击复选框,则会注销