Android跨App数据获取咨询:从数字地址App取位置至TextView
Hey Joe! Let's figure out how to grab that personal digital location from the existing Android app and display it in your TextView. Here's a practical step-by-step guide:
First things first—you need to know how the digital address app lets other apps access its location data. If it has official developer docs, check there for Intent actions, extra keys, or Content Provider details. If not, you can use adb logcat to sniff its behavior: open the digital address app's location page, then run adb logcat | grep Intent in your terminal to catch the Intent it uses.
For this example, let's assume we find:
- Intent Action:
com.digitaladdress.app.GET_PERSONAL_LOCATION - Extra key for location data:
com.digitaladdress.app.EXTRA_LOCATION - Target app package name:
com.digitaladdress.app
We'll use the Activity Result API (since startActivityForResult is deprecated) to launch the target app and retrieve the location data.
First, register a launcher in your Activity:
// Inside your Activity's onCreate method ActivityResultLauncher<Intent> locationLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { // Handle the returned data if (result.getResultCode() == Activity.RESULT_OK) { Intent data = result.getData(); if (data != null) { // Extract the digital location from the Intent extra String digitalLocation = data.getStringExtra("com.digitaladdress.app.EXTRA_LOCATION"); // Populate your TextView with the result yourTextView.setText(digitalLocation); } } } );
Then bind the launcher to your button's click event:
yourButton.setOnClickListener(v -> { Intent intent = new Intent("com.digitaladdress.app.GET_PERSONAL_LOCATION"); // Specify the package to avoid conflicting with other apps intent.setPackage("com.digitaladdress.app"); try { locationLauncher.launch(intent); } catch (ActivityNotFoundException e) { // Show a friendly message if the target app isn't installed Toast.makeText(this, "Please install the digital address app first", Toast.LENGTH_SHORT).show(); } });
Don't forget to cover these potential issues:
- Target app not installed: The
try-catchblock above handles this with a toast. - Null result: Always check if
datais not null to avoid crashes. - Permissions: If the digital address app requires location permissions, make sure your app requests the necessary permissions (like
ACCESS_FINE_LOCATION) or confirm the target app handles permission checks internally.
If the digital address app stores location data in a publicly accessible Content Provider, you can query it directly:
yourButton.setOnClickListener(v -> { Uri locationUri = Uri.parse("content://com.digitaladdress.app/user_location"); String[] projection = {"digital_location"}; // Assumed column name Cursor cursor = getContentResolver().query(locationUri, projection, null, null, null); if (cursor != null) { if (cursor.moveToFirst()) { String location = cursor.getString(cursor.getColumnIndex("digital_location")); yourTextView.setText(location); } cursor.close(); } });
Note: This only works if the target app's Content Provider is configured to allow external access, and you know the correct URI and column names.
The key here is understanding how the digital address app exposes its data. Once you have that info, integrating it into your app is straightforward!
内容的提问来源于stack exchange,提问作者joe

