我正在开发一个应用程序,其中需要 GPS 来检测用户的当前位置并继续进行。所以,我正在使用这个代码:isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
检测 GPS 是否打开,如果它关闭,则提示用户打开它。这段代码可以完美地检测它是否关闭,但是当我打开 GPS 并尝试进一步进行时。上面的代码未能检测到 GPS 已打开,并再次提示用户打开 GPS,即使它已打开。
以下是我在打开 GPS 后尝试进一步进行的方式:
public void btnRetryGpsEvents() {
if (isGPSEnabled) {
ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
locationProvider.getLastKnownLocation()
.subscribe(new Action1<Location>() {
@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 开启时应该执行的代码也没有被执行!
这里出了什么问题?
请告诉我。