如何在Android中为特定应用程序启动位置设置

huangapple go评论64阅读模式
英文:

How to launch the location settings in Android for a specific app

问题

要在Android应用中启用“始终允许”位置权限,请使用以下代码启动应用程序在Android设置中的位置设置:

fun openAppLocationSettings(context: Context) {
    val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
    val packageName = context.packageName
    val uri = Uri.fromParts("package", packageName, null)
    intent.data = uri
    context.startActivity(intent)
}
英文:

To enable "Allow all the time" location permissions in an Android app, how to launch the location settings for the app in the Android settings?

I have seen how to launch the Android settings page for location (all apps), and how to launch the settings for a specific app, but not how to deep link to the location settings for a specific app.

This is what I currently have:

fun openAppLocationSettings(context: Context) {
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        val packageName = context.packageName
        val uri = Uri.fromParts("package", packageName, null)
        intent.data = uri
        context.startActivity(intent)
    }

but it only shows the root page of the app Settings.

答案1

得分: 2

这里的解决方案不是尝试使用意图来启动Android中的位置设置。在Android API >= 30上,Android的工作方式是,您首先需要请求粗略和精确定位的位置权限。在用户允许了权限之后,再发起另一个位置权限请求,用于后台位置。所以:

步骤1:

val permissionsToRequest: Array<String> = arrayOf(
    Manifest.permission.ACCESS_FINE_LOCATION,
    Manifest.permission.ACCESS_COARSE_LOCATION
)
launcher.launch(permissionsToRequest)

步骤2(假设用户在步骤1中允许了粗略/精确定位权限):

val permissionsToRequest: String = Manifest.permission.ACCESS_BACKGROUND_LOCATION
launcher.launch(permissionsToRequest)

但是,与其弹出“始终允许”的位置权限对话框,Android会将用户导航到Android应用的位置设置中。

英文:

The solution here is not to try to use an intent to launch the location settings in Android. The way Android works on Android API >= 30, is that you first need to request location permissions for coarse and fine location. After the user has allowed the permission, then make another location permission request, for background location. So:

Step 1:

val permissionsToRequest: Array&lt;String&gt; = arrayOf(
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
            )
            launcher.launch(permissionsToRequest)

Step 2 (assuming user allows coarse/fine location permission in step 1):

val permissionsToRequest: String =
                Manifest.permission.ACCESS_BACKGROUND_LOCATION
            launcher.launch(permissionsToRequest)

However, instead of a dialog coming up for the "Allow all the time" location permission, Android navigates user to the location settings for the Android app.

答案2

得分: 1

使用这个方法,以便自动弹出允许一直使用的权限请求窗口:

private void getPermission()
{
    progressBar.setVisibility(View.VISIBLE);
    ActivityResultLauncher<String[]> locationPermissionRequest =
            registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), result -> {
                Boolean fineLocationGranted = result.getOrDefault(
                        Manifest.permission.ACCESS_FINE_LOCATION, false);
                Boolean coarseLocationGranted = result.getOrDefault(
                        Manifest.permission.ACCESS_COARSE_LOCATION, false);
                if (fineLocationGranted != null && fineLocationGranted) {
                    // 调用您的方法
                } else if (coarseLocationGranted != null && coarseLocationGranted) {
                    // 仅获得大致位置访问权限。
                    // 调用您的方法
                } else {
                    // 未获得位置访问权限。
                }
            }
    );

    locationPermissionRequest.launch(new String[] {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    });
}
英文:

Use this method so that a pop will automatic come for Allow All The Time..

private void getPermission()
{

    progressBar.setVisibility(View.VISIBLE);
    ActivityResultLauncher&lt;String[]&gt; locationPermissionRequest =
            registerForActivityResult(new ActivityResultContracts
                            .RequestMultiplePermissions(), result -&gt; {
                        Boolean fineLocationGranted = result.getOrDefault(
                                Manifest.permission.ACCESS_FINE_LOCATION, false);
                        Boolean coarseLocationGranted = result.getOrDefault(
                                Manifest.permission.ACCESS_COARSE_LOCATION, false);
                        if (fineLocationGranted != null &amp;&amp; fineLocationGranted) {
                            // Call Your Method
                        } else if (coarseLocationGranted != null &amp;&amp; coarseLocationGranted) {
                            // Only approximate location access granted.
                            // Call Your Method
                        } else {
                            // No location access granted.
                        }
                    }
            );

    locationPermissionRequest.launch(new String[] {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    });
}

答案3

得分: 1

你可以使用以下代码打开 Android 设置中的位置设置:

val locSettings = Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivityForResult(locSettings)

要访问安全设置下的位置隐私设置,可以使用以下代码:

val locSettings = Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS)
startActivityForResult(locSettings)

要在 Kotlin 中实现这一点:

startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))

// 或者

startActivity(Intent(Settings.ACTION_SECURITY_SETTINGS))

编辑:

目前无法打开特定应用的位置设置。相反,您可以打开特定应用的设置屏幕,然后指示用户转到“权限”屏幕。要打开特定应用的设置,可以使用以下代码:

val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivity(intent)
英文:

You can use the following code to open location settings in the Android settings:

Intent locSettings = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(locSettings);

To access the location privacy settings under security settings, you can use the following code:

Intent locSettings = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(locSettings);

To do it in Kotlin:

startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))

//or 

startActivity(Intent(Settings.ACTION_SECURITY_SETTINGS))

Edit:

Opening the location settings for a specific app is not possible as of now. Instead you can open the settings screen for a specific app, and then instruct the user to go to the Permissions screen. To open the settings for a specific app, you can use the following code:

Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts(&quot;package&quot;, getPackageName(), null);
intent.setData(uri);
startActivity(intent);

huangapple
  • 本文由 发表于 2023年5月14日 14:41:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定