限制用户在android中禁用gps选项

时间:2013-02-21 13:41:14

标签: android gps

我的应用程序会经常监控基于GPS的用户位置..现在用户可以手动禁用其设备中的GPS选项...如何限制用户禁用GPS选项。?

1 个答案:

答案 0 :(得分:2)

您可以检查GPS是否已禁用。

LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

boolean isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

然后启动一个对话框,意图打开gps设置,如果isGPSEnabled为false

private void showSettingsGPSAlert() {

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

    // Setting Dialog Title
    alertDialog.setTitle("GPS settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to 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();

}

还要监听位置监听器回调中的更改。

public void onProviderDisabled(String provider) {

         //Do something here. Example tell user that gps is disabled

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

我希望这些有帮助,快乐编码! :)