有没有什么方法可以识别用户授予了哪些位置权限

发布于 2025-01-11 01:57:35 字数 160 浏览 0 评论 0原文

有没有什么方法可以识别用户在 Android 中以编程方式授予了哪个位置权限。

如下所示

  • 使用应用程序时
  • 仅这一次
  • 拒绝

我想从另一个应用程序了解特定应用程序的当前位置权限状态。不是为了检查其运行时间。

Is there any method to identify which location permission is granted by user in programmatically in Android.

like the following

  • While using the app
  • Only this time
  • Deny

I want to know a particular applications current location permission status from another app. Not for checking its run time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2025-01-18 01:57:35

是的,我们可以使用权限组合以编程方式检查它。我根据android版本创建了该方法:

 private void checkLocationPermissionState(Context context){

    int fineLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
    int coarseLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

        int bgLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION);

        boolean isAppLocationPermissionGranted = (bgLocation == PackageManager.PERMISSION_GRANTED) &&
                (coarseLocation == PackageManager.PERMISSION_GRANTED);

        boolean preciseLocationAllowed = (fineLocation == PackageManager.PERMISSION_GRANTED)
                && (coarseLocation == PackageManager.PERMISSION_GRANTED);

        if (preciseLocationAllowed) {
            Log.e("PERMISSION","Precise location is enabled in Android 12");
        } else {
            Log.e("PERMISSION","Precise location is disabled in Android 12");
        }

        if (isAppLocationPermissionGranted) {
            Log.e("PERMISSION","Location is allowed all the time");
        } else if(coarseLocation == PackageManager.PERMISSION_GRANTED){
            Log.e("PERMISSION","Location is allowed while using the app");
        }else{
            Log.e("PERMISSION","Location is not allowed.");
        }

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

        int bgLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION);

        boolean isAppLocationPermissionGranted = (bgLocation == PackageManager.PERMISSION_GRANTED) &&
                (coarseLocation == PackageManager.PERMISSION_GRANTED);

        if (isAppLocationPermissionGranted) {
            Log.e("PERMISSION","Location is allowed all the time");
        } else if(coarseLocation == PackageManager.PERMISSION_GRANTED){
            Log.e("PERMISSION","Location is allowed while using the app");
        }else{
            Log.e("PERMISSION","Location is not allowed.");
        }
        
    } else {

        boolean isAppLocationPermissionGranted = (fineLocation == PackageManager.PERMISSION_GRANTED) &&
                (coarseLocation == PackageManager.PERMISSION_GRANTED);

        if (isAppLocationPermissionGranted) {
            Log.e("PERMISSION","Location permission is granted");
        } else {
            Log.e("PERMISSION","Location permission is not granted");
        }
    }


}

Yes, we can check it programmatically using the combination of permissions. I have created the method on the basis of the android version :

 private void checkLocationPermissionState(Context context){

    int fineLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
    int coarseLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

        int bgLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION);

        boolean isAppLocationPermissionGranted = (bgLocation == PackageManager.PERMISSION_GRANTED) &&
                (coarseLocation == PackageManager.PERMISSION_GRANTED);

        boolean preciseLocationAllowed = (fineLocation == PackageManager.PERMISSION_GRANTED)
                && (coarseLocation == PackageManager.PERMISSION_GRANTED);

        if (preciseLocationAllowed) {
            Log.e("PERMISSION","Precise location is enabled in Android 12");
        } else {
            Log.e("PERMISSION","Precise location is disabled in Android 12");
        }

        if (isAppLocationPermissionGranted) {
            Log.e("PERMISSION","Location is allowed all the time");
        } else if(coarseLocation == PackageManager.PERMISSION_GRANTED){
            Log.e("PERMISSION","Location is allowed while using the app");
        }else{
            Log.e("PERMISSION","Location is not allowed.");
        }

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

        int bgLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION);

        boolean isAppLocationPermissionGranted = (bgLocation == PackageManager.PERMISSION_GRANTED) &&
                (coarseLocation == PackageManager.PERMISSION_GRANTED);

        if (isAppLocationPermissionGranted) {
            Log.e("PERMISSION","Location is allowed all the time");
        } else if(coarseLocation == PackageManager.PERMISSION_GRANTED){
            Log.e("PERMISSION","Location is allowed while using the app");
        }else{
            Log.e("PERMISSION","Location is not allowed.");
        }
        
    } else {

        boolean isAppLocationPermissionGranted = (fineLocation == PackageManager.PERMISSION_GRANTED) &&
                (coarseLocation == PackageManager.PERMISSION_GRANTED);

        if (isAppLocationPermissionGranted) {
            Log.e("PERMISSION","Location permission is granted");
        } else {
            Log.e("PERMISSION","Location permission is not granted");
        }
    }


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文