शेयर्ड यूजर ID के माध्यम से अन्य ऐप्लिकेशन के SharedPreferences पढ़ना
डिफ़ॉल्ट रूप से एंड्रॉइड ऐप्स सैंडबॉक्स्ड होते हैं और एक-दूसरे के SharedPreferences एक्सेस नहीं कर सकते। हालांकि, यदि दोनों ऐप्स एक ही sharedUserId और समान सर्टिफिकेट से साइन हों, तो वे डेटा साझा कर सकते हैं। नोट: एंड्रॉइड 10+ (API 29) में sharedUserId डिप्रिकेटेड है।
पूर्वापेक्षाएँ
- दोनों ऐप्स का
android:sharedUserIdसमान हो - दोनों ऐप्स समान सर्टिफिकेट से साइन हों
- लक्ष्य ऐप का पैकेज नाम ज्ञात हो
चरण 1: पहला ऐप (डेटा लिखने वाला) - Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.app.writer"
android:sharedUserId="com.shared.data">
<application
android:allowBackup="true"
android:label="Writer App">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
चरण 2: पहला ऐप - डेटा लिखें
package com.app.writer;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String PREFS_NAME = "shared_data";
private static final String KEY_EMAIL = "user_email";
private static final String KEY_NAME = "user_name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// डेटा लिखें
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
prefs.edit()
.putString(KEY_EMAIL, "user@example.com")
.putString(KEY_NAME, "राहुल शर्मा")
.apply();
}
}
चरण 3: दूसरा ऐप (डेटा पढ़ने वाला) - Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.app.reader"
android:sharedUserId="com.shared.data">
<application
android:allowBackup="true"
android:label="Reader App">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
चरण 4: दूसरा ऐप - डेटा पढ़ें
package com.app.reader;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView tvEmail, tvName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvEmail = findViewById(R.id.tvEmail);
tvName = findViewById(R.id.tvName);
readSharedPrefs();
}
private void readSharedPrefs() {
try {
// लेखक ऐप का कॉन्टेक्स्ट बनाएं
Context writerContext = createPackageContext(
"com.app.writer",
Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE
);
// SharedPreferences एक्सेस करें
SharedPreferences sharedPrefs = writerContext.getSharedPreferences("shared_data", Context.MODE_PRIVATE);
String email = sharedPrefs.getString("user_email", "नहीं मिला");
String name = sharedPrefs.getString("user_name", "नहीं मिला");
tvEmail.setText("ईमेल: " + email);
tvName.setText("नाम: " + name);
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(this, "लेखक ऐप इंस्टॉल नहीं है", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (SecurityException e) {
Toast.makeText(this, "एक्सेस अस्वीकृत - sharedUserId/सर्टिफिकेट मेल नहीं खाता", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
चरण 5: दूसरा ऐप - लेआउट
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginBottom="8dp"/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>
महत्वपूर्ण: दोनों ऐप्स को समान सर्टिफिकेट से साइन करें
build.gradle में साइनिंग कॉन्फिगर करें:
android {
signingConfigs {
release {
storeFile file("shared_keystore.jks")
storePassword "keystore_password"
keyAlias "shared_key"
keyPassword "key_password"
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
debug {
signingConfig signingConfigs.release // डिबग के लिए भी समान उपयोग करें
}
}
}
आधुनिक विकल्प (SharedUserId के बिना)
एंड्रॉइड 10+ के लिए अनुशंसित दृष्टिकोण:
1. ContentProvider (अनुशंसित)
// लेखक ऐप में ContentProvider बनाएं
public class DataProvider extends ContentProvider {
// query(), insert(), update(), delete() इम्प्लीमेंट करें
// Manifest में export="true" और android:grantUriPermissions="true" सेट करें
}
2. FileProvider (फाइल शेयरिंग के लिए)
// XML में paths परिभाषित करें
<paths>
<files-path name="shared_prefs" path="shared_prefs/" />
</paths>
3. BroadcastReceiver / LocalBroadcastManager
ऐप्स के बीच इवेंट-आधारित संचार के लिए।
4. Room Database + ContentProvider
संरचित डेटा शेयरिंग के लिए सबसे मजबूत समाधान।
चेतावनी: sharedUserId एंड्रॉइड 10 (API 29) से डिप्रिकेटेड है और भविष्य में हटाया जा सकता है। नए ऐप्स के लिए ContentProvider या अन्य IPC मैकेनिज्म का उपयोग करें।