繁体   English   中英

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

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

我发现这个答案的点点滴滴散落在其他帖子中,但我想在这里为其他人录制。

如何简单地请求用户的GPS和/或网络位置,如果他们没有启用该服务,则提示他们这样做?

如果你想在按钮上捕获位置,这就是你如何做到的。 如果用户未启用位置服务,则会将其发送到设置菜单以启用它。

首先,您必须将权限“android.permission.ACCESS_COARSE_LOCATION”添加到清单中。 如果您需要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);
        }
    }
});

我的外部类设置了这个监听器

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) {}

};

然后,无论何时你想停止听这个。 您应该至少在活动的onStop方法中进行此调用。

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

经过Stack Overflow上的大量答案后,我发现这种方法工作得很好,甚至不需要很多代码。
int GPSoff = 0声明为全局变量。
现在,无论您需要检查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