作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
两者都是错误的,虽然我确实检查了 Marshmallow ,但有一些类似运行时权限的东西,所以我点击了这些链接 link1和 link2
并进行了一些调试,并在 isNetworkEnabled 返回 0 之前放置了 checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION);
,即 PERMISSION_GRANTED
代码
SplashScreenActivity
public class SplashScreenActivity extends Activity {
private static final String TAG = "SplashScreenActivity";
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_splashscreen);
int hasLocationPermission = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
if (hasLocationPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
//comes till here . ie hasLocationPermission is true
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION); //breakpoint at this line gives 0 ie PERMISSION_GRANTED
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); //false at debug breakpoint
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);//false at debug breakpoint
}
}
list Here
最佳答案
使用这段代码,它将适用于所有安卓版本-
创建一个 bool 类型的方法来检查用户是否有权限-
private boolean checkPermission() {
boolean flag = true;
String[] permissions = {"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"};
boolean hasPermission = (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissions, com.jeenees.androidapp.utils.Keys.FILE_READ_PERMISSION);
flag = false;
}
} else {
flag = true;
}
return flag;
}
在你想要的地方使用上面的方法-
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
if (checkPermission()){
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); //false at debug breakpoint
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);//false at debug breakpoint
}
希望对您有所帮助。
关于Android Marshmallow,locationManager.isProviderEnbled 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36261217/
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); //getting GPS status
我是一名优秀的程序员,十分优秀!