【问题标题】:fail to get gps/network location even when i set the location permission即使我设置了位置权限,也无法获取 gps/网络位置
【发布时间】:2016-07-23 14:01:18
【问题描述】:

我尝试使用网络/gps 获取位置。

我定义在清单上添加权限(ACCESS_FINE_LOCATION)但我得到的权限是-1(PERMISSION_DENIED)

我在主要活动上调用“GetLocation”类的一个实例。 并从此主要活动中调用“getCurrentLocation(this)”。

代码:

  public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TraceActionLocation t = new TraceActionLocation();

    Location l = t.getCurrentLocation(this);


}


  public class GetLocation
  {
    public Location getCurrentLocation(Context context)
    {
        Location location = null;

    LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

    Boolean isGpsEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    Boolean isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if(!isGpsEnable && !isNetworkEnable)
    {
        // TODO !!! => no gps and no network !!!
    }
    else if(context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_DENIED)
    {
        // 1. get the location from network provider
        if(isNetworkEnable)
        {
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }

        // 2. get the more equate location from the gps
        if(isGpsEnable)
        {
            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }
    }

    return location;
}
}

【问题讨论】:

  • 你实现了什么LocationListener接口?
  • 我决定不实现 LocationListener 接口 - 只询问位置,因为我不需要“LocationListener 接口”上的所有方法
  • 请求用户GRANT权限的代码在哪里?
  • 你在 Marshmallow 上实现这个?
  • 您没有请求授予权限

标签: android


【解决方案1】:

在棉花糖的清单中添加位置权限是不够的。您必须在运行时请求权限。首先,您必须添加 ACCESS_COARSE_LOCATION 权限。正如我之前尝试在运行时请求 ACCESS_FINE_LOCATION 并且这永远不会奏效。 在您的活动中请求 ACCESS_COARSE_LOCATION。在下面找到一个粗略的例子:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i("result", "permissioin granted");

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

}

【讨论】:

  • 添加 ACCESS_COARSE_LOCATION 并解决问题
【解决方案2】:

从移动设备获取用户位置可能很复杂。位置读数(无论来源如何)可能包含错误和不准确的原因有多种。用户位置的一些错误来源包括:

大量的位置来源 GPS、Cell-ID 和 Wi-Fi 都可以提供用户位置的线索。确定使用和信任哪个是准确性、速度和电池效率的权衡问题。 用户移动 由于用户位置发生变化,因此您必须通过每隔一段时间重新估计用户位置来考虑移动。 变化的精度 来自每个位置源的位置估计在其准确性上并不一致。 10 秒前从一个来源获得的位置可能比从另一个或相同来源获得的最新位置更准确。

请求用户权限... ...

【讨论】:

  • 复制android文档不看问题不是解决问题。
猜你喜欢
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
  • 2013-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多