Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में SharedPreferences से डेटा स्टोर, पढ़ें और एडिट कैसे करें?

परिचय

SharedPreferences Android में छोटी मात्रा में प्राइमिटिव डेटा (बूलियन, फ्लोट, इंट, लॉन्ग, स्ट्रिंग) को की-वैल्यू पेयर के रूप में स्टोर करने का एक सरल तरीका है। यह डेटा ऐप रीस्टार्ट के बाद भी बना रहता है। इस ट्यूटोरियल में हम नाम और ईमेल सेव करना, पढ़ना और क्लियर करना सीखेंगे।

आवश्यक चरण

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project से नया प्रोजेक्ट बनाएं।

चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/etEmail"
        android:layout_marginTop="40dp"
        android:ems="10"
        android:hint="नाम"
        android:inputType="text" />

    <EditText
        android:id="@+id/etEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etName"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="ईमेल"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerInParent="true"
        android:onClick="Save"
        android:text="सेव करें" />

    <Button
        android:id="@+id/btnRetrieve"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:onClick="Get"
        android:text="पढ़ें" />

    <Button
        android:id="@+id/btnClear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerInParent="true"
        android:onClick="clear"
        android:text="क्लियर करें" />

</RelativeLayout>

चरण 3: मुख्य गतिविधि (src/MainActivity.java)

package app.com.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText editTextName, editTextEmail;
    public static final String mypreference = "mypref";
    public static final String Name = "nameKey";
    public static final String Email = "emailKey";
    SharedPreferences sharedpreferences;

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

        editTextEmail = findViewById(R.id.etEmail);
        editTextName = findViewById(R.id.etName);

        // SharedPreferences इंस्टेंस प्राप्त करें (प्राइवेट मोड में)
        sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);

        // ऐप शुरू होने पर सेव्ड डेटा लोड करें
        if (sharedpreferences.contains(Name)) {
            editTextName.setText(sharedpreferences.getString(Name, ""));
        }
        if (sharedpreferences.contains(Email)) {
            editTextEmail.setText(sharedpreferences.getString(Email, ""));
        }
    }

    // सेव बटन क्लिक
    public void Save(View view) {
        String strName = editTextName.getText().toString();
        String strEmail = editTextEmail.getText().toString();

        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString(Name, strName);
        editor.putString(Email, strEmail);
        editor.apply(); // एसिंक्रोनस सेव

        Toast.makeText(getApplicationContext(), "सेव हो गया", Toast.LENGTH_SHORT).show();
    }

    // क्लियर बटन क्लिक
    public void clear(View view) {
        editTextName.setText("");
        editTextEmail.setText("");
        Toast.makeText(getApplicationContext(), "क्लियर हो गया", Toast.LENGTH_SHORT).show();
    }

    // पढ़ें बटन क्लिक
    public void Get(View view) {
        sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);

        if (sharedpreferences.contains(Name)) {
            editTextName.setText(sharedpreferences.getString(Name, ""));
        }
        if (sharedpreferences.contains(Email)) {
            editTextEmail.setText(sharedpreferences.getString(Email, ""));
        }
        Toast.makeText(getApplicationContext(), "डेटा प्राप्त हुआ", Toast.LENGTH_SHORT).show();
    }

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

चरण 4: मैनिफेस्ट फ़ाइल (AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

ऐप चलाएं

डिवाइस कनेक्ट करें और Run पर क्लिक करें। नाम और ईमेल दर्ज करें, सेव करें दबाएं, ऐप बंद करके फिर खोलें - डेटा बना रहेगा।

Android में SharedPreferences से डेटा स्टोर, पढ़ें और एडिट कैसे करें? Android में SharedPreferences से डेटा स्टोर, पढ़ें और एडिट कैसे करें?

महत्वपूर्ण बिंदु

  • Context.MODE_PRIVATE - केवल आपका ऐप इस डेटा को एक्सेस कर सकता है।
  • editor.apply() - एसिंक्रोनस (नॉन-ब्लॉकिंग) सेव करता है। commit() सिंक्रोनस है।
  • SharedPreferences बड़े डेटा या कॉम्प्लेक्स ऑब्जेक्ट्स के लिए उपयुक्त नहीं है - उसके लिए Room डेटाबेस उपयोग करें।
  • कुंजी नाम (nameKey, emailKey) यूनिक होने चाहिए।
  1. एंड्रॉइड में डिवाइस ID कैसे प्राप्त करें और स्टोर करें? एंड्रॉइड में डिवाइस ID कैसे प्राप्त करें और स्टोर करें?

    एंड्रॉइड में डिवाइस ID (Android ID) प्राप्त करना और स्टोर करना एंड्रॉइड प्रत्येक डिवाइस के लिए एक यूनिक 64-बिट हेक्साडेसिमल स्ट्रिंग जेनरेट करता है जिसे **Android ID** (SSAID) कहते हैं। यह डिवाइस की पहली बूट पर बनता है और फैक्ट्री रीसेट तक समान रहता है। ध्यान दें: Android 8.0 (API 26) से प्रत्येक ऐप

  1. एंड्रॉइड पर माउस और कीबोर्ड का उपयोग कैसे करें एंड्रॉइड पर माउस और कीबोर्ड का उपयोग कैसे करें

    यदि आप हमारी तरह कुछ भी हैं, तो आप अपने फोन या टैबलेट पर उत्पादक रूप से काम करने के लिए संघर्ष कर सकते हैं। क्या यह सब हमारे दिमाग में है, या माउस और कीबोर्ड का एहसास किसी तरह हमें सीधे जोन में डाल देता है? अधिकांश एंड्रॉइड डिवाइस आपकी उंगली को प्राथमिक इनपुट मेथड के रूप में उपयोग करते हैं। यह एक बड

  1. एंड्रॉइड टीवी पर ADB सेट अप और उपयोग कैसे करें एंड्रॉइड टीवी पर ADB सेट अप और उपयोग कैसे करें

    चाहे आपके पास एंड्रॉइड स्मार्टफोन हो या एंड्रॉइड टीवी, ऑपरेटिंग सिस्टम की प्रकृति समान रहती है। दूसरे शब्दों में, एक एंड्रॉइड टीवी किसी भी अन्य एंड्रॉइड डिवाइस की तरह ही ट्वीक करने योग्य है। यदि आप कुछ गंभीर ट्वीक करने की योजना बना रहे हैं जैसे एंड्रॉइड ऐप साइडलोड करना, तो आपको एंड्रॉइड डिबग ब्रिज,