【问题标题】:Location Service not working fine in some Android devices定位服务在某些 Android 设备中无法正常工作
【发布时间】:2014-08-07 05:20:14
【问题描述】:

如果设备的 GPS 位置被禁用,我将使用以下代码向用户显示一个对话框。

final AlertDialog.Builder builder =
        new AlertDialog.Builder(activity);
        final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
        final String message = "Enable GPS"
        + " service to find current location. And change the location mode to high accuracy for the app to perform accurately. Click OK to go to"
        + " location services settings to let you do so.";

        builder.setMessage(message)
        .setPositiveButton("OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface d, int id) {
                activity.startActivity(new Intent(action));
                d.dismiss();
            }
        })
        .setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface d, int id) {
                d.cancel();
            }
        });
        builder.create().show();

到目前为止,我已经在两台设备 MotoG (v 4.4.4) 和 Samsung duos (v 4.1.2) 上测试了此代码。

我的两台设备都打开了 GPS。 在 MotoG 中,我返回了正确的结果,这是正确的,但在三星设备中,它返回错误。

但是三星设备却返回给我地址(我正在连接到定位服务以获取当前地址)。我在位置客户端的 onConnected 方法中获取地址。如果它返回地址,那么应该启用 GPS 对吗?

为什么有人知道这个问题?

【问题讨论】:

  • 可能是因为启用了网络 GPS 但未启用街道精度。

标签: android gps location


【解决方案1】:

试试这个

public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

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

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // Setting Icon to Dialog
    //alertDialog.setIcon(R.drawable.delete);

    // 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);
            mContext.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();
}

【讨论】:

  • 此代码适用于 android 2.2 。 AFAIK 适用于所有设备。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多