我刚刚开始使用Android编程,到目前为止,我只是对按钮,文本视图和一些活动进行了一些讨论,但我想从服务和GPS位置开始。为了练习一点,我正在构建一个只显示用户坐标的简单应用程序。 我已经使用一些方法创建了自己的服务类,但每当我尝试实现以下方法时,我的应用程序崩溃了:
@TargetApi(23)
public Location getLocation(){
try{
locationManager = (LocationManager)this.ctx.getSystemService(LOCATION_SERVICE);
gpsActivado = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);
}catch(Exception e){
e.printStackTrace();
}
if(gpsActivado && (ctx.checkSelfPermission("ACCESS_FINE_LOCATION") == PackageManager.PERMISSION_GRANTED)){
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
,1000*60
,2
,this);
return locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
}else{
return null;
}
}
通过一些调试消息,我知道问题与检查权限有关。那有什么不对吗?你现在有任何其他方法来检查我是否拥有所需的权限?出于某种原因,我不喜欢我正在使用的那个。
提前致谢:)
PS:我正在使用API 16
答案 0 :(得分:2)
使用Android Marshmallow,即使您在Manifest文件中指定了这些权限,也必须明确请求用户的权限。 因此,您必须以这种方式请求位置权限: 首先,为位置
创建请求代码rpart(a ~ m, data = feature[fold != k, ], ...)
然后检查是否已授予权限,如果没有,则代码将请求权限,该权限将显示要求拒绝/允许位置权限的本机弹出窗口
public static final int LOCATION_REQUEST_CODE = 1001; //Any number
上述代码应在请求任何位置之前写入,最好是在if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_REQUEST_CODE);
} else {
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
,1000*60,2,this);
Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
}
活动中。然后根据用户在弹出窗口中执行的操作,您将获得一个回调,您可以根据自己的要求执行回调。
onCreate()
此外,无论您何时尝试获取该位置,都应检查是否已向您的应用程序授予位置权限,然后获取该位置。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case LOCATION_REQUEST_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED
&& (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
,1000*60,2,this);
Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
}
}
}
}
您可以请求 if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
,1000*60,2,this);
Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
}
或Manifest.permission.ACCESS_FINE_LOCATION
或两者。这取决于你的要求。