使用com.google.android.gms.location.LocationListener时要求用户激活其GPS

时间:2016-04-15 11:38:05

标签: android google-maps gps location

我已经研究了与此相关的所有帖子并阅读了大量文档,但我仍然不知道该怎么做。当我在手机上运行应用程序时,会添加位置按钮,但如果未启用GPS,则在我按下该按钮时没有任何反应。当我启用它(手动,不被推出或任何东西)时,它完美地运行。

我想要做的是当用户按下标准位置按钮时,应该调用新的自定义意图。像这样:

    if (mLastLocation == null) {

    LayoutInflater inflater = getLayoutInflater();
    @SuppressLint("InflateParams") View layout = inflater.inflate(R.layout.toast_gps_view, null);

    Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();


        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);

当我导入android.location.LocationListener时;调用了回调onProviderDisabled方法,我可以在那里添加我的代码,但现在使用com.google.android.gms.location.LocationListener,该方法不再被覆盖。   那么我需要做些什么才能让用户启动GPS?

1 个答案:

答案 0 :(得分:0)

终于找到了怎么做。我是这样做的:

  mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
            @Override
            public boolean onMyLocationButtonClick() {

                LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);

                boolean enabledGPS = service
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);
                boolean enabledWiFi = service
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                if (!enabledGPS) {

                    LayoutInflater inflater = getLayoutInflater();
                    @SuppressLint("InflateParams") View layout = inflater.inflate(R.layout.toast_gps_view, null);

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);
                    toast.show();


                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);

                } else if (!enabledWiFi) {

                    Toast.makeText(getApplicationContext(),
                            "Network signal or WiFi connections not found",
                            Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(
                            Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);

                }

                return false;
            }
        });