我试图使用网络/ 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;
}
}
你实现什么LocationListener的接口? – Eenvincible
我决定不实现LocationListener接口 - 只需询问位置,因为我不需要'LocationListener接口'上的所有方法 – Yanshof
请求用户授予GRANT权限的代码在哪里? – Eenvincible