2011-10-10 29 views

Trả lời

33

Bạn có thể bắt đầu mục đích tùy chọn cho cài đặt vị trí.

Intent gpsOptionsIntent = new Intent( 
    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
startActivity(gpsOptionsIntent); 
+1

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 –

+0

Tại sao nó gây ra một vòng lặp? –

+1

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. –

5

bạn có thể sử dụng

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS); 
     startActivity(intent); 

sau khi kiểm tra gps.

nguồn hoàn chỉnh có thể được tìm thấy HERE hoặc bạn có thể tham khảo SO ANSWER

+0

thay đổi ACTION_LOCATION_SOURCE_SETTINGS thành ACTION_SECURITY_SETTINGS – Xenione

+0

Liên kết hữu ích tới một câu trả lời SO khác! –

8

LOCATION_MODE sẽ LOCATION_MODE_OFF nếu GPS tắt, mà sẽ trả về giá trị 0.

int off = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE); 
    if(off==0){ 
     Intent onGPS = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(onGPS); 
    } 
1

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); 
} 
} 
Các vấn đề liên quan