简体   繁体   English

如何在我的应用程序中启用网络和GPS定位?

[英]how to enable network and gps location from within my app?

I'm trying to get the location of the user: 我正在尝试获取用户的位置:

// getting GPS status //获取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");

I need the user to set enable network location or turn on GPS . 我需要用户设置enable network locationturn on GPS

How can i help the user do each of these options? 我如何帮助用户执行这些选项中的每一个?

  1. open the device settings page and return to my activity on back press. 打开设备设置页面,然后按返回我的活动。

  2. change these two device settings from within my application only? 仅在我的应用程序中更改这两个设备设置?

meaning opening a confirmation dialog that will enable these two settings. 意味着打开一个确认对话框,它将启用这两个设置。

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

well changing these settings in code is not possible anymore after 4.1 (they may be a workarounds to turn it on because on a bug see here but it won't work on 4.3 or 4.4 or you need rooted phones, what you can do is show a dialog to the user to go to settings to enable the GPS 在4.1之后就无法再在代码中更改这些设置了(它们可能是一种解决方法,无法将其打开,因为在此处看到错误但在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();
}

call the previous function in 在中调用上一个函数

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

then in your onActivityResult you can check code 0 if the GPS has been turned on or not 然后在您的onActivityResult您可以检查代码0(是否打开了GPS)

With the latest location api you can prompt user to change location setting dynamically through setting dialog inside the application. 使用最新的位置api,您可以提示用户通过应用程序内的设置对话框动态更改位置设置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM