У меня есть два приложения. Одно — приложение администратора, а другое — гостевое.
- Профиль пользователя 1 [приложение администратора установлено]
- Профиль пользователя 2[ Гостевое приложение установлено]
Теперь я перехожу на User2 и устанавливаю гостевое приложение для чтения вставленных данных от пользователя User1.
Фактическое поведение:
Но проблема в том, что я не могу прочитать вставленные данные от пользователя User2.
Ожидаемое поведение :
Мне нужно прочитать вставленные данные в профиль пользователя 1.
Когда я сохраняю оба приложения в одном профиле пользователя [профиль пользователя 1], я могу прочитать данные.< /p>
Ниже приведен фрагмент кода:
Администратор:
public class MainActivity extends AppCompatActivity {
private LinearLayout guestLayout;
private EditText adminUsernameEditText, adminAgeEditText;
private TextView displayTextView;
private int guestCount = 0;
private static final Uri SHARED_DATA_URI = Uri.parse("content://com.admin.guest.profile.provider/shared_data");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
guestLayout = findViewById(R.id.guestLayout);
adminUsernameEditText = findViewById(R.id.adminUsernameEditText);
adminAgeEditText = findViewById(R.id.adminAgeEditText);
displayTextView = findViewById(R.id.displayTextView);
Button adminButton = findViewById(R.id.adminButton);
Button addGuestButton = findViewById(R.id.addGuestButton);
adminButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveAdminData();
}
});
addGuestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addGuestButton();
}
});
}
@SuppressLint("SetTextI18n")
private void saveAdminData() {
String username = adminUsernameEditText.getText().toString().trim();
String ageText = adminAgeEditText.getText().toString().trim();
if (username.isEmpty()) {
adminUsernameEditText.setError("Username cannot be empty");
return;
}
if (ageText.isEmpty()) {
adminAgeEditText.setError("Age cannot be empty");
return;
}
int age;
try {
age = Integer.parseInt(ageText);
} catch (NumberFormatException e) {
adminAgeEditText.setError("Age must be a number");
return;
}
ContentValues values = new ContentValues();
values.put("username", username);
values.put("age", age);
getContentResolver().insert(SHARED_DATA_URI, values);
displayTextView.setText("Admin data saved: Username - " + username + ", Age - " + age);
}
private void addGuestButton() {
guestCount++;
Button guestButton = new Button(this);
guestButton.setText("Guest " + guestCount);
guestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayAdminData();
}
});
guestLayout.addView(guestButton);
}
private void displayAdminData() {
ContentResolver contentResolver = getContentResolver();
StringBuilder displayText = new StringBuilder("Admin Data:\n");
try (Cursor cursor = contentResolver.query(SHARED_DATA_URI, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
do {
@SuppressLint("Range") String username = cursor.getString(cursor.getColumnIndex("username"));
@SuppressLint("Range") int age = cursor.getInt(cursor.getColumnIndex("age"));
displayText.append("Username: ").append(username)
.append(", Age: ").append(age).append("\n");
} while (cursor.moveToNext());
} else {
displayText.append("No data found.");
}
}
displayTextView.setText(displayText.toString());
}
}
Я успешно вставил данные в приложение администратора для профиля пользователя 1.
Ниже находится GuestApp для чтения введенных данных в приложении администратора для профиля пользователя 2. .
public class MainActivity extends AppCompatActivity {
private TextView displayTextView;
private static final Uri SHARED_DATA_URI = Uri.parse("content://com.admin.guest.profile.provider/shared_data");
private static final String TAG = "GuestActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayTextView = findViewById(R.id.displayTextView);
displayAdminData();
}
private void displayAdminData() {
ContentResolver contentResolver = getContentResolver();
StringBuilder displayText = new StringBuilder("Admin Data:\n");
try (Cursor cursor = contentResolver.query(SHARED_DATA_URI, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
do {
@SuppressLint("Range") String username = cursor.getString(cursor.getColumnIndex("username"));
@SuppressLint("Range") int age = cursor.getInt(cursor.getColumnIndex("age"));
displayText.append("Username: ").append(username)
.append(", Age: ").append(age).append("\n");
Log.d(TAG, "Fetched data - Username: " + username + ", Age: " + age);
} while (cursor.moveToNext());
} else {
displayText.append("No data found.");
Log.d(TAG, "No data found in ContentProvider.");
}
}
displayTextView.setText(displayText.toString());
}
}
Результат:
Невозможно прочитать данные из профиля пользователя 1.
В манифесте я дал этот запрос для Android 12, но до сих пор я не умею читать
Подробнее здесь: https://stackoverflow.com/questions/792 ... er-android
Мобильная версия