Thực hiện LocationListner: vì vậy
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider)
{
if (provider.equals(LocationManager.GPS_PROVIDER))
{
showGPSDiabledDialog();
}
}
Hiển thị hộp thoại để mở Cài đặt:
public void showGPSDiabledDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("GPS Disabled");
builder.setMessage("Gps is disabled, in order to use the application properly you need to enable GPS of your device");
builder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), GPS_ENABLE_REQUEST);
}
}).setNegativeButton("No, Just Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
mGPSDialog = builder.create();
mGPSDialog.show();
}
Trong OnCreate Method cuộc gọi của bạn:
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
return;
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Và ovveride onActivityResult phương pháp để kiểm tra nếu người dùng kích hoạt một cách chính xác gps
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == GPS_ENABLE_REQUEST)
{
if (mLocationManager == null)
{
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
showGPSDiabledDialog();
}
}
else
{
super.onActivityResult(requestCode, resultCode, data);
}
}
Nguồn
2017-07-18 16:56:25
nhưng điều này sẽ gây ra vòng lặp trong màn hình cài đặt nơi bất cứ khi nào bạn đi trở lại nó sẽ vẫn đưa đến màn hình cài đặt –
Tại sao nó gây ra một vòng lặp? –
Tôi nghĩ rằng nó phụ thuộc vào nơi bạn đặt mã này. Nếu bạn bắt đầu Hoạt động bất cứ khi nào ứng dụng của bạn tiếp tục, bạn có thể kết thúc trong một vòng lặp. Nhưng nó không phải do chính mã đó gây ra. –