साझा किए गए संदर्भों का उपयोग करना एंड्रॉइड में कुंजी-मूल्य जोड़ी डेटा को सहेजने और पुनर्प्राप्त करने का तरीका है और पूरे एप्लिकेशन में सत्र को रखने के लिए भी एक तरीका है।
SharedPreferences आप निम्नलिखित कदम उठाने की आवश्यकता के साथ काम करने के लिए: निर्माता को दो तर्क पारित करके
शेयर SharedPreferences इंटरफ़ेस का प्रारंभ (स्ट्रिंग और पूर्णांक)
// Sharedpref file name
private static final String PREF_NAME = "PreName";
// Shared pref mode
int PRIVATE_MODE = 0;
// साझा किए गए संदर्भों को शुरू करना साझा किए गए संदर्भ pref = getAplicationContext()। GetSharedPreferences ("MyPref", 0); // 0 - निजी मोड के लिए
संपादक इंटरफ़ेस और SharedPreferences मूल्यों को संशोधित करने के तरीकों का उपयोग करना और भविष्य के पुनः प्राप्ति के लिए डेटा भंडारण
//Initialising the Editor
Editor editor = pref.edit();
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString के लिए ("KEY_NAME" "स्ट्रिंग मान") ; // भंडारण स्ट्रिंग editor.putInt ("key_name", "int value"); // भंडारण पूर्णांक editor.putFloat ("key_name", "float value"); // भंडारण editor.putLong ("key_name", "लंबा मान"); // लंबे समय तक भंडारण
editor.commit(); // प्रतिबद्ध परिवर्तन
Retrieving Data
डाटा GetString बुला() (स्ट्रिंग के लिए) विधि द्वारा बचाया वरीयताओं से प्राप्त किया जा सकता है। याद रखें कि इस विधि को साझा प्राथमिकताओं पर कॉल किया जाना चाहिए, संपादक पर नहीं।
// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
- अंत में, समाशोधन/लॉगआउट के मामले में की तरह डेटा को हटाने
आप साझा वरीयताओं आप निकालना कॉल कर सकते हैं ("KEY_NAME" से हटाना चाहते हैं) उस विशेष मूल्य को हटाने के लिए। आप सभी डेटा हटाना चाहते हैं, फोन स्पष्ट()
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes
Following will clear all the data from shared preferences
editor.clear();
editor.commit(); // commit changes
एक सरल और सीधा उदाहरण डाउनलोड करने के लिए नीचे दिए गए लिंक का पालन करें: http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
मुझे आशा है कि मैं नियमों के खिलाफ नहीं गए और इस लिंक को साझा करने के लिए मंच के नियम।
//This is my SharedPreferences Class
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class SessionManager {
// Shared Preferences
SharedPreferences pref;
// 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 = "AndroidHivePref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static final String KEY_NAME = "name";
// Email address (make variable public to access from outside)
public static final String KEY_EMAIL = "email";
// Constructor
@SuppressLint("CommitPrefEdits")
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
*/
public void createLoginSession(String name, String email) {
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_NAME, name);
// Storing email in pref
editor.putString(KEY_EMAIL, email);
// commit changes
editor.commit();
}
/**
* 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, MainActivity.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);
}
}
/**
* Get stored session data
*/
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
// user name
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
// user email id
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
// return user
return user;
}
/**
* Clear session details
*/
public void logoutUser() {
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, MainActivity.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);
}
/**
* Quick check for login
**/
// Get Login State
public boolean isLoggedIn() {
return pref.getBoolean(IS_LOGIN, false);
}
}
Finally, you will have to create an instance of this SessionManager class in the onCreate method of your activity class and then call the createLoginSession for the session that will be used throughout the entire session
// Session Manager Class
SessionManager session;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Session Manager
session = new SessionManager(getApplicationContext());
session.createLoginSession("Username", intentValue);
}
मुझे आशा है कि इससे मदद मिलती है।
आप कृपया मुझे कम से कम एक नमूना कार्यक्रम – Rohith
भेज सकते हैं जब मैं यह कर और वापस प्रवेश करने का प्रयास है, यह रिटर्न शून्य – Sauron
थाई बहुत असुरक्षित है, क्योंकि आप साझा किए गए प्राथमिकताओं को रूट किए गए फोन पर xml फ़ाइल के रूप में एक्सेस कर सकते हैं – Choletski