【发布时间】: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