在安卓 6 中运行"Targeting SDK 22"应用 (SDK 23)



我注意到,当有人试图从android 6(SDK 23)访问我的android应用程序时,我会遇到以下问题:

java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.

由于修复此问题需要时间,我决定将我的应用程序目标SDK降级为SDK22

我的问题是:

  1. 是否可以在android 6(SDK 23)中运行"SDK 22目标"应用程序
  2. 上面SecurityException的解决方案是什么?*我已经在我的Manifiest中授予了ACCESS_FINE_LOCATION权限

首先,设备应该向后兼容(尽管这不是一个笼统的声明)Android 6.0 API,以使用更低的SDK运行应用程序。您需要确保您的ide中安装了SDK 22。

另一个是关于android 6的权限有额外的问题。

请参阅此处:

在运行时请求权限

下面的例子检查权限

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.WRITE_CALENDAR);

然后请求您需要的权限

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) {
    // Show an expanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.
    } else {
    // No explanation needed, we can request the permission.
    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant. The callback method gets the
    // result of the request.
    }
}

这个答案包含了更全面的解释。

https://stackoverflow.com/a/32084038/3956566

最新更新