gpt4 book ai didi

Android Marshmallow,locationManager.isProviderEnbled 总是返回 false

转载 作者:行者123 更新时间:2023-11-29 17:21:58 25 4
gpt4 key购买 nike

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

//getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

两者都是错误的,虽然我确实检查了 Marshmallow ,但有一些类似运行时权限的东西,所以我点击了这些链接 link1link2

并进行了一些调试,并在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com