1 回答
TA贡献1785条经验 获得超8个赞
就像评论中提到的 Martin Zeitler 一样,您必须为 Marshmallow 上方和下方的 API 做好准备,例如:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 123);
} else {
//GPS method
}
} else {
//GPS method
}
就像你在 onRequestResult 的问题中提到的一样:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 123: {
// If request is cancelled, the result arrays are empty
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//GPS method
} else {
// Toast for user
}
return;
}
}
}
添加回答
举报