LocationSettingsRequest对话框启用GPS - onActivityResult()已跳过

时间:2015-07-05 22:22:46

标签: android dialog

我的应用程序的一部分需要位置服务,因此如果当前关闭位置,应用程序将提示用户启用它。 Here就是我这样做的方式:(另见this Stack Overflow答案)

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);

PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

result.setResultCallback(new ResultCallback<LocationSettingsResult>() 
{
     @Override
     public void onResult(LocationSettingsResult result) 
     {
         final Status status = result.getStatus();
         final LocationSettingsStates = result.getLocationSettingsStates();
         switch (status.getStatusCode()) 
         {
             case LocationSettingsStatusCodes.SUCCESS:
                 // All location settings are satisfied. The client can initialize location
                 // requests here.
                 ...
                 Log.d("onResult", "SUCCESS");
                 break;
             case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                 // Location settings are not satisfied. But could be fixed by showing the user
                 // a dialog.
                 Log.d("onResult", "RESOLUTION_REQUIRED");
                 try 
                 {
                     // Show the dialog by calling startResolutionForResult(),
                     // and check the result in onActivityResult().
                     status.startResolutionForResult(OuterClass.this, REQUEST_LOCATION);
                 } 
                 catch (SendIntentException e) 
                 {
                     // Ignore the error.
                 }
                 break;
             case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                 // Location settings are not satisfied. However, we have no way to fix the
                 // settings so we won't show the dialog.
                 ...
                 Log.d("onResult", "UNAVAILABLE");
                 break;
         }
     }
 });

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    // This log is never called
    Log.d("onActivityResult()", Integer.toString(resultCode));

    final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
    switch (requestCode)
    {
        case REQUEST_LOCATION:
            switch (resultCode)
            {
                case Activity.RESULT_OK:
                {
                    // All required changes were successfully made
                    break;
                }
                case Activity.RESULT_CANCELED:
                {
                    // The user was asked to change settings, but chose not to
                    break;
                }
                default:
                {      
                    break;
                }
            }
            break;
    }
}

此代码运行良好,但始终会跳过onActivityResult()。无论用户是否从YesNo按下backDialogonActivityResult()onActivityResult()都无法运行。

我需要Android来调用onActivityResult(),所以如果用户选择不打开位置服务,我可以适当地处理它。

Google的开发者页面(以及上面的代码)明确指出应该调用final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);。有人知道为什么会被跳过吗?

我也不知道这条线的目的是什么:

onResume()

谢谢!

编辑:关于我的应用程序结构的基本信息:

  • 此代码包含在Fragment的{​​{1}}方法中,该方法实现GoogleApiClient.ConnectionCallbacksGoogleApiClient.OnConnectionFailedListenerLocationListener以接收位置更新。示例见here
  • onLocationChanged()中,Fragment将有一个自定义View来电invalidate(),并使用更新的信息重新绘制。

11 个答案:

答案 0 :(得分:109)

看起来主要的问题是你拥有Fragment中的所有代码,并且由于startResolutionForResult()需要传递一个Activity,所以Activity是获得onActivityResult()回调的。

解决这个问题的一种方法是使用here描述的技术,在结果出现时从Activity中手动调用Fragment的onActivityResult()方法。

我刚才有一个简单的例子。

首先,Activity添加了Fragment,并且还具有将onActivityResult()的结果传递给Fragment的功能:

public class MainActivity extends AppCompatActivity{

    LocationFragment lFrag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lFrag = LocationFragment.newInstance();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, lFrag).commit();

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == LocationFragment.REQUEST_LOCATION){
            lFrag.onActivityResult(requestCode, resultCode, data);
        }
        else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

这是Fragment,它包含显示对话框和处理结果的所有功能。在这个简单的例子中,我只使用Toast消息来验证它是否按预期工作。请注意,我在您的问题代码中所做的主要更改是使用getActivity()来获取调用startResolutionForResult()所需的活动参考。

public class LocationFragment extends Fragment
        implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {


    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    PendingResult<LocationSettingsResult> result;
    final static int REQUEST_LOCATION = 199;

    public static LocationFragment newInstance() {
        LocationFragment fragment = new LocationFragment();
        return fragment;
    }

    public LocationFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();
        mGoogleApiClient.connect();

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_location, container, false);
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(30 * 1000);
        mLocationRequest.setFastestInterval(5 * 1000);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest);
        builder.setAlwaysShow(true);

        result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                //final LocationSettingsStates state = result.getLocationSettingsStates();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        // All location settings are satisfied. The client can initialize location
                        // requests here.
                        //...
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be fixed by showing the user
                        // a dialog.
                        try {
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            status.startResolutionForResult(
                                    getActivity(),
                                    REQUEST_LOCATION);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Location settings are not satisfied. However, we have no way to fix the
                        // settings so we won't show the dialog.
                        //...
                        break;
                }
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        Log.d("onActivityResult()", Integer.toString(resultCode));

        //final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
        switch (requestCode)
        {
            case REQUEST_LOCATION:
                switch (resultCode)
                {
                    case Activity.RESULT_OK:
                    {
                        // All required changes were successfully made
                        Toast.makeText(getActivity(), "Location enabled by user!", Toast.LENGTH_LONG).show();
                        break;
                    }
                    case Activity.RESULT_CANCELED:
                    {
                        // The user was asked to change settings, but chose not to
                        Toast.makeText(getActivity(), "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show();
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                break;
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

}

以下是视觉上的结果,如果禁用了位置模式,首先显示对话框:

enter image description here

然后,如果用户单击否,结果将从活动传递到片段,片段显示Toast:

enter image description here

当用户单击“是”但成功结果并启用“位置模式”时,同样如此:

enter image description here

请注意,在Activity中保留所有这些功能可能是更好的选择,然后在结果进入时调用Fragment中的公共方法。

这是完整的工作代码,用于保持Activity中的功能。 当然,在此解决方案中,您需要在Fragment中添加一个调用,以在调用onActivityResult()后更新位置模式的状态。

public class MainActivity extends AppCompatActivity
        implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {


    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    PendingResult<LocationSettingsResult> result;
    final static int REQUEST_LOCATION = 199;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();
        mGoogleApiClient.connect();

    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(30 * 1000);
        mLocationRequest.setFastestInterval(5 * 1000);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest);
        builder.setAlwaysShow(true);

        result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                //final LocationSettingsStates state = result.getLocationSettingsStates();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        // All location settings are satisfied. The client can initialize location
                        // requests here.
                        //...
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be fixed by showing the user
                        // a dialog.
                        try {
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            status.startResolutionForResult(
                                    MainActivity.this,
                                    REQUEST_LOCATION);
                        } catch (SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Location settings are not satisfied. However, we have no way to fix the
                        // settings so we won't show the dialog.
                        //...
                        break;
                }
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        Log.d("onActivityResult()", Integer.toString(resultCode));

        //final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
        switch (requestCode)
        {
            case REQUEST_LOCATION:
                switch (resultCode)
                {
                    case Activity.RESULT_OK:
                    {
                        // All required changes were successfully made
                        Toast.makeText(MainActivity.this, "Location enabled by user!", Toast.LENGTH_LONG).show();
                        break;
                    }
                    case Activity.RESULT_CANCELED:
                    {
                        // The user was asked to change settings, but chose not to
                        Toast.makeText(MainActivity.this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show();
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                break;
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }
}

答案 1 :(得分:32)

您需要将此添加到结果回调中:

case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
    try {
        fragment.startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CHECK_SETTINGS, null, 0, 0, 0, null);
    } catch (IntentSender.SendIntentException e) {
        // Ignore the error.
    }
    break;
将在您的片段上调用

onActivityResult,您无需在活动中手动调用它。这基本上是startResolutionForResult的工作原理。

答案 2 :(得分:7)

当您需要解决 StatusResolvableApiException 时,我建议您利用 activity.registerForActivityResult API 代替 startResolutionForResult

ActivityResultLauncher<IntentSenderRequest> launcher = activity.registerForActivityResult(
        new ActivityResultContracts.StartIntentSenderForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    // All required changes were successfully made
                } else {
                    // The user was asked to change settings, but chose not to
                }
            }
        });

IntentSenderRequest intentSenderRequest = new IntentSenderRequest.Builder(exception.getResolution()).build();
launcher.launch(intentSenderRequest);

您正在使用 Java,但如果需要 Kotlin:

val launcher = activity.registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            // User accepted
        } else {
            // User didn't accepted
        }
    }

val intentSenderRequest = IntentSenderRequest.Builder(exception.resolution).build()
launcher.launch(intentSenderRequest)

答案 3 :(得分:4)

如果您希望结果返回到片段而不是使用

startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CODE_LOCATION_SETTING, null, 0, 0, 0, null);

代替status.startResolutionForResult(YourActivity, LOCATION_REQUEST);

使用上述方法只会将结果传递回片段。

答案 4 :(得分:0)

我看到您对请求代码使用了不同的常量REQUEST_CHECK_SETTINGSREQUEST_LOCATION。他们有相同的价值吗?

代码:final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
上述代码的目的是在更改设置后获取位置设置的当前状态(如使用网络,GPS,...)。
此外,在您的代码中,我认为它应该是LocationSettingsStates.fromIntent(data);,因为intent在这里并不存在,也许它只是一个错字。

答案 5 :(得分:0)

因为片段中存在所有谷歌api代码。尝试以下它将有助于克服...

1.为片段创建一个空构造函数。

2.在onCreateView()...

之前使用oncreate()方法

3.在oncreate()....

中加入Google api代码
    public mainFragment(){

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        buildGoogleApiClient();
        buildLocationSettingsRequest();

        checkLocationSettings();
        mGoogleApiClient.connect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  

供您参考......

Click here...

答案 6 :(得分:0)

在活动中保存片段字段(如Daniel所建议的)通常不是一个好的决定,因为想象你有多个片段,每个片段都包含位置代码。我是以不同的方式做到的:

public class MainActivity extends Activity implements PlaceFragment.SettingsModifyHandler {

    private static final int LOCATION_SETTINGS_RESULT = 1;
    private OnResultCallback placeCallback;

    ...

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == LOCATION_SETTINGS_RESULT) {
            if (resultCode == Activity.RESULT_OK) {
                placeCallback.resultOk();
            } else {
                placeCallback.resultFail();
            }
        placeCallback = null;
        }
    }

    @Override
    public void handle(IntentSender intentSender, OnResultCallback callback) {
        placeCallback = callback;
        try {
            startIntentSenderForResult(intentSender, LOCATION_SETTINGS_RESULT, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            callback.resultFail();
        }
    }
}

public class PlaceFragment extends Fragment {

    private SettingsModifyHandler settingsModifyHandler;

    ...

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (context instanceof SettingsModifyHandler) {
            settingsModifyHandler = (SettingsModifyHandler) context;
        } else {
            throw new RuntimeException("Parent activity must implement PlaceFragment.SettingsModifyHandler interface");
        }
    }

    /* Callback from module, where you implemented status.getStatusCode().LocationSettingsStatusCodes.RESOLUTION_REQUIRED case
    (status is instance of com.google.android.gms.common.api.Status)
    You provide intentSender here through status.getResolution().getIntentSender() */
    @Override
    public void placeLoadError(IntentSender sender) {
        TextView view_text = (TextView) root.findViewById(R.id.text_error);
        TextView view_btn = (TextView) root.findViewById(R.id.btn_reply);

        view_text.setText("Need to change location settings");
        view_btn.setText("Change");
        view_btn.setOnClickListener(v -> {
            settingsModifyHandler.handle(sender, new SettingsModifyHandler.OnResultCallback() {
                @Override
                public void resultOk() {
                    presenter.loadPlace(placeId);
                }

                @Override
                public void resultFail() {
                    ToastUtils.show("You should change location settings!");
                }
            });
        });
    }

    public interface SettingsModifyHandler {
        void handle(IntentSender intentSender, OnResultCallback callback);

        interface OnResultCallback {
            void resultOk();
            void resultFail();
        }
    }
}

答案 7 :(得分:0)

源代码

https://drive.google.com/open?id=0BzBKpZ4nzNzUOXM2eEhHM3hOZk0

build.gradle文件中的依赖关系

compile&#39; com.google.android.gms:play-services-location:7.8.0&#39;

清单文件中的权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>


package com.keshav.enablelocationwithokcancelbuttoncontrol;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class LocationAddress
{
    private static final String TAG = "LocationAddress";

    public static void getAddressFromLocation(final double latitude, final double longitude,
                                              final Context context, final Handler handler) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                String result = null;
                try {
                    List<Address> addressList = geocoder.getFromLocation(
                            latitude, longitude, 1);
                    if (addressList != null && addressList.size() > 0) {
                        Address address = addressList.get(0);
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                            sb.append(address.getAddressLine(i)).append("\n");
                        }
                        sb.append(address.getLocality()).append("\n");
                        sb.append(address.getPostalCode()).append("\n");
                        sb.append(address.getCountryName());
                        result = sb.toString();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Unable connect to Geocoder", e);
                } finally {
                    Message message = Message.obtain();
                    message.setTarget(handler);
                    if (result != null) {
                        message.what = 1;
                        Bundle bundle = new Bundle();
                        result = "Latitude: " + latitude + " Longitude: " + longitude +
                                "\n\nAddress:\n" + result;
                        bundle.putString("address", result);
                        message.setData(bundle);
                    } else {
                        message.what = 1;
                        Bundle bundle = new Bundle();
                        result = "Latitude: " + latitude + " Longitude: " + longitude +
                                "\n Unable to get address for this lat-long.";
                        bundle.putString("address", result);
                        message.setData(bundle);
                    }
                    message.sendToTarget();
                }
            }
        };
        thread.start();
    }
}

package com.keshav.enablelocationwithokcancelbuttoncontrol;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;

public class GPSTracker extends Service implements LocationListener
{

    private final Context mContext;

    // flag for GPS status
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

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

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }

                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);

                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     * */

    public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(GPSTracker.this);
        }
    }

    /**
     * Function to get latitude
     * */

    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }

        // return latitude
        return latitude;
    }

    /**
     * Function to get longitude
     * */

    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }

        // return longitude
        return longitude;
    }

    /**
     * Function to check GPS/wifi enabled
     * @return boolean
     * */

    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    /**
     * Function to show settings alert dialog
     * On pressing Settings button will lauch Settings Options
     * */

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

        // Setting Dialog Title
        alertDialog.setTitle("GPS is 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);
                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();
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}


package com.keshav.enablelocationwithokcancelbuttoncontrol;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class LocationAddress
{
    private static final String TAG = "LocationAddress";

    public static void getAddressFromLocation(final double latitude, final double longitude,
                                              final Context context, final Handler handler) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                String result = null;
                try {
                    List<Address> addressList = geocoder.getFromLocation(
                            latitude, longitude, 1);
                    if (addressList != null && addressList.size() > 0) {
                        Address address = addressList.get(0);
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                            sb.append(address.getAddressLine(i)).append("\n");
                        }
                        sb.append(address.getLocality()).append("\n");
                        sb.append(address.getPostalCode()).append("\n");
                        sb.append(address.getCountryName());
                        result = sb.toString();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Unable connect to Geocoder", e);
                } finally {
                    Message message = Message.obtain();
                    message.setTarget(handler);
                    if (result != null) {
                        message.what = 1;
                        Bundle bundle = new Bundle();
                        result = "Latitude: " + latitude + " Longitude: " + longitude +
                                "\n\nAddress:\n" + result;
                        bundle.putString("address", result);
                        message.setData(bundle);
                    } else {
                        message.what = 1;
                        Bundle bundle = new Bundle();
                        result = "Latitude: " + latitude + " Longitude: " + longitude +
                                "\n Unable to get address for this lat-long.";
                        bundle.putString("address", result);
                        message.setData(bundle);
                    }
                    message.sendToTarget();
                }
            }
        };
        thread.start();
    }
}

答案 8 :(得分:0)

下面的片段中用于处理启用位置的代码是可以使用的最新代码。现已弃用设置API。下面是使用SettingsClientAPI的方法。

我还注意到,即使在用户启用位置的情况下,在Android 10设备中也是如此; onActivityResult中的状态结果为RESULT_CANCELED,我找不到在Android 10设备中摆脱该问题的方法,而在Android PIE中结果代码为RESULT_OK。因此,检测用户是否启用它的唯一方法是通过使用LocationManagerCompat API(适用于Android 10设备)显式检查是否启用了位置

private fun enableLocationIfRequired() {
    val builder = LocationSettingsRequest.Builder()
        .addLocationRequest(LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY))
        .setAlwaysShow(true)

    val settingsClient = LocationServices.getSettingsClient(context!!)

    val task = settingsClient!!.checkLocationSettings(builder.build())
    task.addOnCompleteListener {
        try {
            val response = it.getResult(ApiException::class.java)

            //Success
            Log.d(javaClass.simpleName, "Location is enabled")

        } catch (exception: ApiException) {
            Log.d(javaClass.simpleName, "exception thrown: ${exception.statusCode}")
            when (exception.statusCode) {
                LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
                    // Location settings are not satisfied. But could be fixed by showing the
                    // user a dialog.
                    try {
                        // Cast to a resolvable exception.
                        val resolvable = exception as ResolvableApiException
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        Log.d(javaClass.simpleName, "startResolutionForResult called")

                        this.startIntentSenderForResult(
                            resolvable.resolution.intentSender,
                            RC_LOCATION_ENABLE,
                            null, 0, 0, 0, null
                        )

                    } catch (e: IntentSender.SendIntentException) {
                        // Ignore the error.
                        Log.d(javaClass.simpleName, "IntentSender.SendIntentException")
                    } catch (e: ClassCastException) {
                        // Ignore, should be an impossible error.
                        Log.d(javaClass.simpleName, "ClassCastException")
                    }
                }
                LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                }
            }
        }
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    when (requestCode) {
        RC_LOCATION_ENABLE -> {
            if (resultCode == Activity.RESULT_OK) {
                Log.d(javaClass.simpleName, "Location is enabled by user")
            } else {
                Log.d(javaClass.simpleName, "Location enable request is cancelled by user")
            }
            val lm = context!!.getSystemService(LOCATION_SERVICE) as LocationManager
            if (LocationManagerCompat.isLocationEnabled(lm)) {
                Log.d(javaClass.simpleName, "Location is enabled by user")
            } else {
                Log.d(javaClass.simpleName, "Location enable request is cancelled by user")
            }
        }
    }

}

答案 9 :(得分:0)

感谢@gianlucaparadise solution,您应该为新 API 编写代码:

片段(或者可能是活动):

private lateinit var checkLocationSettings: ActivityResultLauncher<IntentSenderRequest>

override fun onCreate(savedInstanceState: Bundle?) {
    checkLocationSettings = 
        registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                // GPS is turned on in system settings.
            }
    }
}

要启用 GPS 的片段或实用程序类(请参阅 12):

.addOnFailureListener(context) { e ->
    when ((e as? ApiException)?.statusCode) {
        LocationSettingsStatusCodes.RESOLUTION_REQUIRED ->
            try {
                // Cast to a resolvable exception.
                val resolvable = e as ResolvableApiException
                // Old API: show the dialog by calling startResolutionForResult(),
                // and check the result in onActivityResult().
                // New API: call registerForActivityResult::launch
                // and check the result in callback.
                val intentSenderRequest =
                    IntentSenderRequest.Builder(resolvable.resolution).build()
                checkLocationSettings.launch(intentSenderRequest)
            } catch (sie: IntentSender.SendIntentException) {
                Timber.e("GPS: Unable to execute request.")
            } catch (cce: java.lang.ClassCastException) {
                // Ignore, should be an impossible error.
                Timber.e("GPS: Unable to execute request, ClassCastException.")
            }

已弃用 Fragment 和 onActivityResult 的 API 变体:LocationSettingsRequest dialog to enable GPS - onActivityResult() skipped

答案 10 :(得分:0)

对于 Kotlin 用户

通过在 Activity 中进行以下更改,此解决方案适用于 FragmentcheckLocationSetting()

对于活动 resolvableApiException.startResolutionForResult(this@MainActivity, REQUEST_CHECK_SETTING)

对于片段 startIntentSenderForResult(resolvableApiException.resolution.intentSender, REQUEST_CHECK_SETTING, null, 0, 0,0,null)

通过使用 LocationSettingsResponse 可以完成这个任务。

MainActivity.kt


    private fun checkLocationSetting()
        {
            locationRequest = LocationRequest.create()
            locationRequest.apply {
                priority=LocationRequest.PRIORITY_HIGH_ACCURACY
                interval = 5000
                fastestInterval = 2000
            }
    
            val builder = LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest)
            builder.setAlwaysShow(true)
    
            val result: Task<LocationSettingsResponse> = LocationServices.getSettingsClient(applicationContext)
                .checkLocationSettings(builder.build())
    
            result.addOnCompleteListener {
                try{
                    val response: LocationSettingsResponse = it.getResult(ApiException::class.java)
                    Toast.makeText(this@MainActivity, "GPS is On", Toast.LENGTH_SHORT).show()
                    Log.d(TAG, "checkSetting: GPS On")
                }catch(e:ApiException){
    
                    when(e.statusCode){
                        LocationSettingsStatusCodes.RESOLUTION_REQUIRED ->{
                            val resolvableApiException = e as ResolvableApiException
                            resolvableApiException.startResolutionForResult(this@MainActivity, REQUEST_CHECK_SETTING)
                            Log.d(TAG, "checkSetting: RESOLUTION_REQUIRED")
                        }
    
                        LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
                            // USER DEVICE DOES NOT HAVE LOCATION OPTION
                        }
                    }
                }
            }
        }

onActivityResult

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode)
        {
            REQUEST_CHECK_SETTING ->{
                when(resultCode){
                    Activity.RESULT_OK->{
                        Toast.makeText(this@MainActivity, "GPS is Turned on", Toast.LENGTH_SHORT).show()
                    }
                    Activity.RESULT_CANCELED ->{
                        Toast.makeText(this@MainActivity, "GPS is Required to use this app", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }

链接到完整代码 MainActivity.kt

输出:

链接到完整代码 MainActivity.kt