Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.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 清除所有SharedReferences android_Java_Android_Sharedpreferences_Logout - Fatal编程技术网

Java 清除所有SharedReferences android

Java 清除所有SharedReferences android,java,android,sharedpreferences,logout,Java,Android,Sharedpreferences,Logout,我正在将我的用户登录详细信息保存到我的SharedReferences文件中。问题是,当我试图澄清这些陈词滥调时,细节并不清楚。当下一个用户登录时,不会显示其详细信息 这是我的密码:- Save.java sessionManager.createLoginSession(username, deviceId, name ); SessionManagement.java 共享参考优先 // Editor for Shared preferences Editor ed

我正在将我的用户登录详细信息保存到我的SharedReferences文件中。问题是,当我试图澄清这些陈词滥调时,细节并不清楚。当下一个用户登录时,不会显示其详细信息

这是我的密码:-

Save.java

sessionManager.createLoginSession(username, deviceId,  name );
SessionManagement.java

共享参考优先

   // Editor for Shared preferences
        Editor editor;

        // Context
        Context _context;

        // Shared pref mode
        int PRIVATE_MODE = 0;

        // Sharedpref file name
        private static final String PREF_NAME = "UserDetails";

    public SessionManagement(Context context)
        {
            this._context = context;
            pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
            editor = pref.edit();
        }

        public void createLoginSession(String emailId, String deviceid,String usersname)
        {
            // Storing login value as TRUE
            editor.putBoolean(IS_LOGIN, true);

            editor.putBoolean(KEY_DEVICEREGISTERED, true);
            editor.putString(KEY_EMAILID, emailId);

            editor.putString(KEY_DEVICEiD, deviceid);

            editor.putString(KEY_USERSNAME, usersname);


            // commit changes
            editor.commit();
        }

        /**
         * Get stored session data
         * */
        public HashMap<String, String> getUserDetails()
        {
            HashMap<String, String> user = new HashMap<String, String>();

            user.put(KEY_EMAILID, pref.getString(KEY_EMAILID, null));

            user.put(KEY_DEVICEiD, pref.getString(KEY_DEVICEiD, null));

            user.put(KEY_USERSNAME, pref.getString(KEY_USERSNAME, null));


            // return user
            return user;
        }
        /**
         * Check login method wil check user login status
         * If false it will redirect user to login page
         * Else won't do anything
         * */
        public void checkLogin()
        {
            // Check login status
            if(!this.isLoggedIn())
            {
                // user is not logged in redirect him to Login Activity
                Intent i = new Intent(_context, Login.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
                  _context.startActivity(i);
            }

        }

       // This function clears all session data and redirect the user to LoginActivity
        /**
             * Clear session details
             * */
            public void logoutUser()
            {
                // Clearing all data from Shared Preferences
                //editor.clear();



                editor.remove(KEY_DEVICEiD);
                editor.remove(KEY_EMAILID);

                editor.remove(KEY_USERSNAME);

                editor.commit();

                // After logout redirect user to Loing Activity
                Intent i = new Intent(_context, Login.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
                _context.startActivity(i);
            }

            public boolean isLoggedIn()
            {
                return pref.getBoolean(IS_LOGIN, false);
            }

完成()

我正在使用这个结构

public void logoutUser() {
    // Clearing all data from Shared Preferences
    editor.putBoolean(IS_LOGIN, false);
    editor.putString(KEY_TOKEN, "");
    editor.putString(KEY_EMAIL, "");
    editor.commit();
}

不是remove,而是为所有preferenceditor.remove(KEY_DEVICEiD.commit())设置空值;editor.remove(KEY_EMAILID).commit();remove(KEY_USERSNAME.commit();有点超出了这个范围,因为我相信上一个答案解决了您的问题,我会小心您存储在SharedPrefs中的内容,因为任何具有根访问权限的设备都可以在那里导航,并以普通xml文件的形式打开prefs,并查看您的用户凭据。我建议您对存储在那里的数据进行加密,如果它是私有的编辑器。clear().commit();软件Sainath我尝试了你的方法,但仍然有相同的结果我也在我的应用程序中使用共享prreferences,以便将其用于船上帐户管理的帐户。因此,我使用了如下所述的三个类:有谁能给我一个提示,为什么我不能更改一次创建的帐户?找不到页面,请告诉我更多详细信息:D@MhmdAljobairi仍然存在相同的问题场景是:1-当用户登录时,必须将用户名和密码放在mSharedPreferences上。2-用户注销时必须清除mSharedPreferences,但删除mSharedPreferences值。找不到我的页面?我可以导航到它。你不是吗?
public void logoutUser() {
    // Clearing all data from Shared Preferences
    editor.putBoolean(IS_LOGIN, false);
    editor.putString(KEY_TOKEN, "");
    editor.putString(KEY_EMAIL, "");
    editor.commit();
}
 /**
         * Clear session details
         * */
        public void logoutUser(){

            // Clearing all user data from Shared Preferences
            editor.clear();
            editor.commit();


            // After logout redirect user to Login Activity
            Intent i = new Intent(_context, LoginActivity.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
            _context.startActivity(i);
        }

Try the above code.Hope it will work.