0

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

4

1 回答 1

2

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

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

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

如果 isGPSEnabled 为 false,则打开一个对话框,意图打开 gps 设置

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();

}

还要监听 Location 监听器回调的变化。

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

}

我希望这些对您有所帮助,祝您编码愉快!:)

于 2013-02-21T13:49:18.510 回答