我正在开发一个应用程序,其中需要GPS来检测用户的当前位置并继续进行。因此,我使用此代码:isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);来检测GPS是否打开,并在用户关闭时提示用户将其打开。此代码完美检测是否处于关闭状态,但是当我打开GPS并尝试继续进行时。上述代码未能检测到GPS处于开启状态,并且即使在ON时也再次提示用户打开GPS。为什么'locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);'无法检测GPS是否打开或不在android棉花糖?
以下是我正在尝试接通GPS后进一步进行:
public void btnRetryGpsEvents() {
if (isGPSEnabled) {
ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
locationProvider.getLastKnownLocation()
.subscribe(new Action1() {
@Override
public void call(Location location) {
currentLatDouble = location.getLatitude();
currentLngDouble = location.getLongitude();
}
});
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (currentLatDouble != null || currentLngDouble != null) {
retrieveHWrapper();
btnRetryGps.setVisibility(View.INVISIBLE);
gps_off.setVisibility(View.INVISIBLE);
Toast.makeText(getBaseContext(), "Loading h-requests...", Toast.LENGTH_LONG).show();
} else {
gps_off.setVisibility(View.VISIBLE);
progressBarLoadingRequests.setVisibility(View.INVISIBLE);
btnRetryGps.setVisibility(View.VISIBLE);
btnRetryGps.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnRetryGpsEvents();
}
});
}
}
}, 2000);
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
// Setting Dialog Title
alertDialog.setTitle("GPS settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Enable it in settings menu.");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
gps_off.setVisibility(View.VISIBLE);
progressBarLoadingRequests.setVisibility(View.INVISIBLE);
btnRetryGps.setVisibility(View.VISIBLE);
btnRetryGps.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnRetryGpsEvents();
}
});
}
}
我已经要求允许在onCreate()方法:
int hasWriteContactsPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_SETTINGS);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_SETTINGS)) {
showMessageOKCancel("You need to allow access to few permissions so that the app can work as expected.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_SETTINGS},
REQUEST_RUNTIME_PERMISSION);
}
});
return;
}
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_SETTINGS},
REQUEST_RUNTIME_PERMISSION);
return;
}
}
所以,底线即使打开GPS并点击“重试”按钮,在GPS开启时本应执行的代码仍未执行!
这里怎么回事?
请让我知道。