2016-07-23 130 views
0

我试图使用网络/ gps获取位置。即使我设置了位置权限,也无法获得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; 
} 
} 
+0

你实现什么LocationListener的接口? – Eenvincible

+0

我决定不实现LocationListener接口 - 只需询问位置,因为我不需要'LocationListener接口'上的所有方法 – Yanshof

+0

请求用户授予GRANT权限的代码在哪里? – Eenvincible

回答

2

增加对位置的许可清单中的棉花糖是不够的。您将不得不在运行时请求权限。首先,您必须添加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 
    } 
} 

}

+0

也加入ACCESS_COARSE_LOCATION并且它解决问题 – Yanshof

-2

从移动设备获取用户位置可能是复杂的。有几个原因可能会导致位置读数(不论来源)是否包含错误并且不准确。用户位置中的一些错误来源包括:

许多位置信息源 GPS,小区识别码和Wi-Fi都可以提供用户位置的线索。确定使用和信任是精确度,速度和电池效率的权衡问题。 用户移动 由于用户位置发生变化,您必须经常通过重新估计用户位置来解释移动。 变化的准确性 来自每个位置源的位置估计的准确性不一致。 10秒前从一个源获得的位置可能比来自另一个源或同一个源的最新位置更精确。

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

+0

复制android文档并且未读取问题并未解决问题。 – Yanshof