简体   繁体   English

如果禁用,Android获取位置或提示以启用位置服务

[英]Android get location or prompt to enable location service if disabled

I've found bits and pieces of this answer scattered through other posts, but I wanted to record it here for others. 我发现这个答案的点点滴滴散落在其他帖子中,但我想在这里为其他人录制。

How can I simply request the user's GPS and/or Network location and, if they haven't enabled the service, prompt them to do so? 如何简单地请求用户的GPS和/或网络位置,如果他们没有启用该服务,则提示他们这样做?

If you want to capture the location on a button push, here's how you'd do it. 如果你想在按钮上捕获位置,这就是你如何做到的。 If the user does not have a location service enabled, this will send them to the settings menu to enable it. 如果用户未启用位置服务,则会将其发送到设置菜单以启用它。

First, you must add the permission "android.permission.ACCESS_COARSE_LOCATION" to your manifest. 首先,您必须将权限“android.permission.ACCESS_COARSE_LOCATION”添加到清单中。 If you need GPS (network location isn't sensitive enough), add the permission "android.permission.ACCESS_FINE_LOCATION" instead, and change the "Criteria.ACCURACY_COARSE" to "Criteria.ACCURACY_FINE" 如果您需要GPS(网络位置不够灵敏),请改为添加权限“android.permission.ACCESS_FINE_LOCATION”,并将“Criteria.ACCURACY_COARSE”更改为“Criteria.ACCURACY_FINE”

Button gpsButton = (Button)this.findViewById(R.id.buttonGPSLocation);
gpsButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // Start loction service
        LocationManager locationManager = (LocationManager)[OUTERCLASS].this.getSystemService(Context.LOCATION_SERVICE);

        Criteria locationCritera = new Criteria();
        locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
        locationCritera.setAltitudeRequired(false);
        locationCritera.setBearingRequired(false);
        locationCritera.setCostAllowed(true);
        locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);

        String providerName = locationManager.getBestProvider(locationCritera, true);

        if (providerName != null && locationManager.isProviderEnabled(providerName)) {
            // Provider is enabled
            locationManager.requestLocationUpdates(providerName, 20000, 100, [OUTERCLASS].this.locationListener);
        } else {
            // Provider not enabled, prompt user to enable it
            Toast.makeText([OUTERCLASS].this, R.string.please_turn_on_gps, Toast.LENGTH_LONG).show();
            Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            [OUTERCLASS].this.startActivity(myIntent);
        }
    }
});

My outer class has this listener set up 我的外部类设置了这个监听器

private final LocationListener locationListener = new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        [OUTERCLASS].this.gpsLocationReceived(location);
    }

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onProviderEnabled(String provider) {}

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

};

Then, whenever you want to stop listening call this. 然后,无论何时你想停止听这个。 You should at least make this call during your activity's onStop method. 您应该至少在活动的onStop方法中进行此调用。

LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(this.locationListener);

After going through a lot of answers on Stack Overflow, I found this method to work perfectly fine and doesn't even require a lot of code. 经过Stack Overflow上的大量答案后,我发现这种方法工作得很好,甚至不需要很多代码。
Declare int GPSoff = 0 as a global variable. int GPSoff = 0声明为全局变量。
Now wherever you need to check for the present status of the GPS and re-direct the user to turn the GPS on, use this : 现在,无论您需要检查GPS的当前状态并重新指示用户打开GPS,请使用以下命令:

try {
                GPSoff = Settings.Secure.getInt(getContentResolver(),Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
            }
            if (GPSoff == 0) {
                showMessageOKCancel("You need to turn Location On",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent onGPS = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                startActivity(onGPS);
                            }
                        });
            }

要提示用户启用位置服务,您应该使用google play服务中包含的新LocationRequest,您可以在LocationRequest中找到完整的指南

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

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