【问题标题】:how to enable network and gps location from within my app?如何从我的应用程序中启用网络和 gps 位置?
【发布时间】:2014-06-12 18:51:36
【问题描述】:

我正在尝试获取用户的位置:

// 获取 GPS 状态

isGPSEnabled = locationManage.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
            Log.e(MyLogger.TAG, "Non providers are enabled");

我需要用户设置enable network locationturn on GPS

我如何帮助用户完成这些选项?

  1. 打开设备设置页面并在后按时返回我的活动。

  2. 仅在我的应用程序中更改这两个设备设置?

意思是打开一个确认对话框来启用这两个设置。

【问题讨论】:

标签: java android gps geolocation android-location


【解决方案1】:

在 4.1 之后不再可能在代码中更改这些设置(它们可能是打开它的一种解决方法,因为存在错误 see here 但它不适用于 4.3 或 4.4,或者您需要有根手机,您可以做的是向用户显示一个对话框以转到设置以启用 GPS

public static void LocationAlertDialog(final Context context) {

    AlertDialog.Builder d = new AlertDialog.Builder(context);
    d.setTitle("No Providers");
    d.setMessage("Unfortunately, you don't have any providers to get your location, would you like to turn on your GPS?");

    d.setNegativeButton(context.getResources().getString(R.string.dialogCancel), new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        }
    });

    d.setPositiveButton(context.getResources().getString(R.string.dialogAccept), new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivityForResult(i, 0);
        }
    });

    d.show();
}

调用前面的函数

if (!isGPSEnabled && !isNetworkEnabled) {
        LocationAlertDialog(this);
        Log.e(MyLogger.TAG, "Non providers are enabled");

然后在您的onActivityResult 中,您可以检查代码 0 是否已打开 GPS

【讨论】:

    【解决方案2】:

    startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);

    【讨论】:

    • 我只能在我的应用程序中更改这两个设备设置吗?
    • 它会随着你安装的所有应用程序而改变,你不能只改变一个应用程序的位置配置
    • 不,我的意思是如何在我的应用程序中更改所有已安装应用程序的全局设置 - 静默 - 无需将用户带到设置页面
    • 使用我编写的代码,它会将您带到位置设置页面,并在更改后返回您的应用程序,然后更改所有应用程序的位置设置。如果你想创建自己的位置配置视图(不使用默认的android)我不认为你可以实现这一点,也许有办法要求超级用户权限,但我不知道
    • 很久以前是gps的这种方法,但它是一个错误code.google.com/p/android/issues/detail?id=7890
    【解决方案3】:

    使用最新的位置API,您可以通过应用程序内的设置对话框提示用户动态更改位置设置。

    https://developer.android.com/training/location/change-location-settings.html

    【讨论】:

      猜你喜欢
      • 2013-05-15
      • 2010-11-06
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 2014-10-24
      相关资源
      最近更新 更多