使用共享prference在android中维护会话

时间:2016-09-20 07:31:13

标签: android session sharedpreferences

我正在开发一个应用程序,其中我使用 SharedPreference 维护会话,因此当用户点击退出按钮时,它只会离开应用程序。但问题是当用户在手机中关闭最近的应用程序时,在我们的应用程序中它再次显示登录屏幕。我不知道该怎么做。请帮忙。      下面是我的mainActivity代码:

    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();

            }

1 个答案:

答案 0 :(得分:0)

这是一个简单的解决方案。

用户永远不会使用此功能从最近关闭您的应用。他需要从活动中关闭它。检查是否有效

<activity
android:name="youActivity"
android:theme="@android:style/Theme.NoDisplay"
android:autoRemoveFromRecents="true"/>

android:autoRemoveFromRecents="true"可以帮助你