在询问位置

时间:2016-01-01 07:05:39

标签: java android google-maps-android-api-2 android-permissions runtime-permissions

目前,在第一次启动我的应用时询问运行时权限时,它会提示用户使用其位置。如果你点击是,它就不会像现在这样启用位置。

但是,如果我重新启动应用程序,它会启用该位置。有关我可以在首次启动时启用位置的建议吗?代码的第一部分在OnCreate中调用。

    if (ContextCompat.checkSelfPermission(MapsActivity.this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // 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(MapsActivity.this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    REQUEST_CODE_ASK_PERMISSIONS);

        }
    }

  @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
        if (permissions.length == 1 &&
                permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                mMap.setMyLocationEnabled(true);
            } else {
                // Show rationale and request permission.
            }
            mMap.setMyLocationEnabled(true);
        } else {
            //permission denied
        }
    }
}

  public void onMapReady(GoogleMap googleMap) {

    int permissionCheck = ContextCompat.checkSelfPermission(MapsActivity.this,
            Manifest.permission.ACCESS_FINE_LOCATION);

3 个答案:

答案 0 :(得分:1)

如果您在模拟器上进行测试,则只会在第一次运行时询问该权限。 How to debug Android 6.0 permissions?

此外,如果您没有完全卸载应用程序并且正在从较低的SDK升级,或者用户已在运行时设置了权限并将其设置为默认值,则不会再次询问运行时权限。

尝试完全卸载应用,或检查默认设置并清除它们。

答案 1 :(得分:0)

仔细看看这一行

DataTable table = new DataTable();
DataRow row;
conn.Open();
da.Fill(table);
int nProducts = table.Rows.Count;
products = new product[nProducts];
for (int i = 0; i < nProducts; i++)
{
    row = table.Rows[i];
    pdname = row["name"].ToString();
    product p = new product(pdname);
    p.PDname = pdname;
    products[i] = p;
}

permissions是一个String数组,意味着你应该使用equals()进行比较 或compareTo()方法。

答案 2 :(得分:0)

如果此代码位于片段中,则不会调用OnRequestPermissionResult()。因此,在您重新打开应用程序之前,不会调用setMyLocationEnabled。

要解决此更改:

ActivityCompat.requestPermissions(MapsActivity.this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                REQUEST_CODE_ASK_PERMISSIONS);

要:

requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                REQUEST_CODE_ASK_PERMISSIONS);

ActivityCompat.requestPermissions适用于活动。使用Fragment时,您应该直接调用requestPermissions。