如何在操作栏

时间:2017-12-27 17:17:41

标签: java android sharedpreferences

我需要帮助。我的应用程序中有操作栏。我希望我的注销内容。我怎样才能做到这一点?我已经有了这些东西。但是我没有那个session.java。我试图复制其他人的代码,但它显示了一些错误。请帮我。谢谢你们!

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            logout();
            return true;
        }
        return super.onOptionsItemSelected(item);
  }
  private void logout() {
        session.logout();
        startActivity(new Intent(this, MainActivity.class));
        finish();
  }

2 个答案:

答案 0 :(得分:0)

你需要使用共享首选项来保持会话,我认为session.logout();这个方法是在一个类中创建的,它们使用共享首选项来保持会话。好的,现在我会告诉你如何保持登录会话以及如何退出。

第一步:当您尝试登录并通过JSON Api或sqlite数据库获得成功响应时,只需使用共享首选项并在那里设置正标志值。

setp two:然后当您在onOptionsItemSelected方法中点击工具栏中的退出按钮时,只需将该共享首选项的值更改为否定。

答案 1 :(得分:0)

Like this sir?



public class LoginActivity extends AppCompatActivity {


        SharedPreferences sharedpreferences;

        public static final String my_shared_preferences = "my_shared_preferences";
        public static final String session_status = "session_status";
        Boolean session = false;

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


            final TextView mShowDialog =(TextView) findViewById(R.id.txtShowDialog);
            final TextView tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);

            //Check session login if TRUE then go directly MainActivity
            sharedpreferences = getSharedPreferences(my_shared_preferences, Context.MODE_PRIVATE);
            session = sharedpreferences.getBoolean(session_status, false);


            if (session) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                finish();
                startActivity(intent);
            }



            tvRegisterLink.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
                    LoginActivity.this.startActivity(registerIntent);
                }
            });
            mShowDialog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder mbuilder = new AlertDialog.Builder(LoginActivity.this);
                    View mView = getLayoutInflater().inflate(R.layout.dialog_login,null);
                     final EditText hehe=  mView.findViewById(R.id.etUsername);
                    final EditText hehe1=  mView.findViewById(R.id.etPassword);
                    final Button bLogin =  mView.findViewById(R.id.bSignIn);

                   bLogin.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final String Username = hehe.getText().toString();
                             final String Password = hehe1.getText().toString();

                            // Response received from the server
                            Response.Listener<String> responseListener = new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonResponse = new JSONObject(response);
                                        boolean success = jsonResponse.getBoolean("success");

                                        if (success) {

                                            SharedPreferences.Editor editor = sharedpreferences.edit();
                                            editor.putBoolean(session_status, true);
                                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                            LoginActivity.this.startActivity(intent);
                                        } else {
                                            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                                            builder.setMessage("Login Failed")
                                                    .setNegativeButton("Retry", null)
                                                    .create()
                                                    .show();
                                        }

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };

                            LoginRequest loginRequest = new LoginRequest(Username, Password, responseListener);
                            RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
                            queue.add(loginRequest);
                        }
                    });
                    mbuilder.setView(mView);
                    AlertDialog dialog = mbuilder.create();
                    dialog.show();

                }
            });


        }
    }