4

Android Fusedlocationproviderclient 不适用于 APN Sim(这意味着私人互联网,并非所有人都可以访问)它正在使用 Wifi 或普通互联网(这意味着全球移动互联网),我也尝试了位置管理器(播放服务不启用这些设备)。

活动课

    import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
//import android.location.LocationListener;
import android.location.LocationManager;

import android.location.LocationProvider;
import android.os.PowerManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationAvailability;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnCanceledListener;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.SuccessContinuation;
import com.google.android.gms.tasks.Task;


import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

import java.util.Locale;

public class LocationActivity extends AppCompatActivity implements LocationListener {


    private FusedLocationProviderClient mFusedLocationClient;

    private double wayLatitude = 0.0, wayLongitude = 0.0;
    private LocationRequest locationRequest;
    private LocationCallback locationCallback;
    private Button btnLocation;
    private TextView txtLocation;
    private Button btnContinueLocation;
    private TextView txtContinueLocation;
    private StringBuilder stringBuilder;
    private boolean isContinue = false;
    private int LOCATION_REQUEST = 1000;

    GPSTracker gps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        this.txtContinueLocation = (TextView) findViewById(R.id.txtContinueLocation);
        this.btnContinueLocation = (Button) findViewById(R.id.btnContinueLocation);
        this.txtLocation = (TextView) findViewById(R.id.txtLocation);
        this.btnLocation = (Button) findViewById(R.id.btnLocation);


        try {


            locationRequest = LocationRequest.create();
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            locationRequest.setInterval(10 * 1000); // 10 seconds
            locationRequest.setFastestInterval(5 * 1000); // 5 seconds
            locationCallback = new LocationCallback() {

                @Override
                public void onLocationAvailability(LocationAvailability locationAvailability) {
                    super.onLocationAvailability(locationAvailability);
                    try {
                        if (!locationAvailability.isLocationAvailable()) {
                        GoogleApiAvailability api = GoogleApiAvailability.getInstance();
                        Task<Void> task = api.makeGooglePlayServicesAvailable(LocationActivity.this);
                        task.addOnCompleteListener(LocationActivity.this, new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                              //  getLocation();
                              //  mFusedLocationClient.removeLocationUpdates(this);
                                gps = new GPSTracker(LocationActivity.this);

                                // Check if GPS enabled
                                if (gps.canGetLocation()) {

                                    wayLatitude = gps.getLatitude();
                                    wayLongitude = gps.getLongitude();

                                    // \n is for new line
                                    Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + wayLatitude + "\nLong: " + wayLongitude, Toast.LENGTH_LONG).show();
                                } else {
                                    Toast.makeText(getApplicationContext(), "No location", Toast.LENGTH_LONG).show();
                                }
                            }
                        });

                        task.addOnFailureListener(LocationActivity.this, new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(LocationActivity.this, "Playservice failure", Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                               //init();


                   //     }
                    } catch (SecurityException e) {

                    }
                }

                @Override
                public void onLocationResult(LocationResult locationResult) {
                    if (locationResult == null) {
                        return;
                    }
                    for (Location location : locationResult.getLocations()) {
                        if (location != null) {
                            wayLatitude = location.getLatitude();
                            wayLongitude = location.getLongitude();
//                        if (!isContinue) {
//                            txtLocation.setText(String.format(Locale.US, "%s - %s", wayLatitude, wayLongitude));
//                        } else {
//                            stringBuilder.append(wayLatitude);
//                            stringBuilder.append("-");
//                            stringBuilder.append(wayLongitude);
//                            stringBuilder.append("\n\n");
//                            txtContinueLocation.setText(stringBuilder.toString());
                            Toast.makeText(LocationActivity.this, wayLatitude + " _ " + wayLongitude, Toast.LENGTH_LONG).show();
                            // }
//                        if (mFusedLocationClient != null) {
//                            mFusedLocationClient.removeLocationUpdates(locationCallback);
//                        }
                        }
                    }
                }

            };


            btnLocation.setOnClickListener(v -> {
                // checkGooglePlayServices();

                getLocation();
            });

            btnContinueLocation.setOnClickListener(v -> {
                //  checkGooglePlayServices();
//                PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//                boolean isPowerSaveMode = pm.isPowerSaveMode();
//                Log.e("TAG", "Mode " + isPowerSaveMode);
//
                //  init();
                getLocation();
                //  _getLocation();
            });
            //   getLocation();
            mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        } catch (Exception e) {
            Log.e("TAG", e.getMessage());
        }

    }

    private void getLocation() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                    LOCATION_REQUEST);

        } else {
            getLatLang();

            // init();
        }
    }

    public void getLatLang() {
        try {

            mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
            mFusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {

                    if (location != null) {
                        wayLatitude = location.getLatitude();
                        wayLongitude = location.getLongitude();
                        Toast.makeText(LocationActivity.this, wayLatitude + " _ " + wayLongitude, Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(LocationActivity.this, "Location not get Fused client", Toast.LENGTH_LONG).show();
                    }
                }
            });
            mFusedLocationClient.getLastLocation().addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    wayLatitude = 0.0;
                    wayLongitude = 0.0;
                    Toast.makeText(LocationActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();

                }
            });
            mFusedLocationClient.getLastLocation().addOnCanceledListener(this, new OnCanceledListener() {
                @Override
                public void onCanceled() {
                    Log.e("TAG", "loc cancel");
                }
            });
        } catch (Exception e) {
            Log.e("TAG", "loc exce" + e.getMessage());
        }
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1000: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    getLatLang();
                    //   init();
                } else {
                    Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
                }
                break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //  mFusedLocationClient.removeLocationUpdates(locationCallback);
//        if(locMgr!=null)
//            locMgr.removeUpdates();
    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            wayLatitude = location.getLatitude();
            wayLongitude = location.getLongitude();
            Toast.makeText(LocationActivity.this, wayLatitude + " _ " + wayLongitude + "  CHANGE", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(LocationActivity.this, "Location not get Fused client chnage locATION", Toast.LENGTH_LONG).show();
        }
    }
    }

HELPER - 如果位置没有得到,那么我们尝试所有三个提供者,如果任何提供者给出结果,那么我们跳过其他提供者。

public class GPSTracker extends Service implements LocationListener {
    private final Context mContext;
    // flag for GPS status
    boolean isGPSEnabled = false;

    boolean isPassive = 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 = 1; // 10 meters
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 1; // 1 minute
    // Declaring a Location Manager
    protected LocationManager locationManager;

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

    @SuppressLint("MissingPermission")
    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);
            isPassive = locationManager
                    .isProviderEnabled(LocationManager.PASSIVE_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled && !isPassive) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                // First get location from Network Provider

                if (isPassive) {
                    // if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.PASSIVE_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Toast.makeText(mContext, "Location Try Passive Provider", Toast.LENGTH_SHORT);
                    Log.e("Passive Enabled", "Passive Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        } else {
                            Toast.makeText(mContext, "Location Not get Passive Provider", Toast.LENGTH_SHORT);
                        }
                    }
                }
                // }.

                // 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);
                        Toast.makeText(mContext, "Location Try GPS Provider", Toast.LENGTH_SHORT);
                        Log.e("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);

                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                Log.e("TAG", "GPS " + latitude + " vjhg " + longitude);
                            } else {
                                Toast.makeText(mContext, "Location Not get GPS Provider", Toast.LENGTH_SHORT);
                            }
                        }
                    }
                }

                if (isNetworkEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                        Log.e("Network", "Network Enabled");
                        Toast.makeText(mContext, "Location Try Network Provider", Toast.LENGTH_SHORT);
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            } else {
                                Toast.makeText(mContext, "Location Not get Network Provider", Toast.LENGTH_SHORT);
                            }
                        }
                    }
                }


            }

        } 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) {
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            Log.e("TAG", "Change " + latitude + " ``` " + longitude);
        }
    }

    @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;
    }
}

显现

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <!--    <uses-permission android:name="android.permission.ACCESS_" />-->
    <uses-feature
        android:name="android.hardware.location.gps"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.location.network"
        android:required="false" />

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:screenOrientation="portrait"
        android:supportsRtl="true">

这是我的示例代码。一旦我连接了我的wifi,它就可以工作了,但我的意思是我们想使用APN sim。

4

1 回答 1

3

答: Google 位置服务使用 Google 的混合位置数据来更快地确定设备的位置。

来自:网络位置提供商是否需要互联网来确定位置?以及 Google IO 2019 视频:使用新的 FusedLocationProvider 随时随地无缝顺畅地定位,其中提供了有关 FusedLocationProvider 的详细信息。

GPS 仍然有效,只是获取位置的速度较慢,并且在户外效果最佳。AGPS(辅助 GPS)需要移动连接来帮助 GPS 定位。如果您想在室内或城市环境中定位,您需要使用混合定位的外部服务,该服务使用 Wi-Fi、蓝牙,可能还有手机信号塔?但至少需要频繁连接到外部服务,因为位置数据库会不断更新(即 WiFi 热点发生变化,新结构可以改变无线电模式),因此与在专用网络上访问 Google 服务的问题相同。

我需要将 [Google 服务] 列入白名单的 google 网址是什么?

来自:FCM 端口和您的防火墙

对于传出连接,FCM 不提供特定 IP,因为我们的 IP 范围更改过于频繁,而且您的防火墙规则可能会过时,从而影响您的用户体验。理想情况下,您会将端口 5228-5230 列入白名单,没有 IP 限制。但是,如果您必须有 IP 限制,则应将Google 的 ASN 15169中列出的 IPv4 和 IPv6 块中的所有 IP 地址列入白名单。这是一个很大的列表,您应该计划每月更新您的规则。由防火墙 IP 限制引起的问题通常是间歇性的并且难以诊断。

为传入消息打开的端口:

5228

5229

5230

允许传出连接的端口:

其中之一(首选选项#1):

  1. 无IP限制
  2. Google 的 ASN 15169中列出的 IP 块中包含的所有 IP 地址。不要忘记至少每月更新一次。

虽然 Google 服务不是 Firebase Cloud Messaging,但它在相同的不断变化的全球基础设施上,因此在没有特定文档说明的情况下是合理的匹配。

还鉴于 Google Maps 不是 Google Location Services,请参阅此问题中的 Google Maps 相关资源:Using google maps api behind firewall

备择方案

另一种选择是不为您的混合位置提供商使用 Google 服务。有像HERE这样的第三方供应商,似乎有HERE 服务提供的高级高级定位服务。您必须检查适合您使用的计划,并与他们联系以了解哪些 IP 地址/域需要被列入白名单,如果未包含在哪些 IP 地址允许防火墙访问 geocoder.api.here 中。 com?

可能还有其他提供商(例如Skyhook)来调查它们是否符合您的需求。我会看看 APN Sim 提供商是否有服务。或者,如果所有设备都由单个制造商提供,请查看该制造商是否拥有专有(非 Google)定位服务。

以上任何一项都做不到

如果企业无法允许上述选项,则应用程序仅限于在 GPS 可用时修复 GPS 的速度较慢。GPS 是免费的,但要获得更快的定位时间或改善城市/室内位置需要一些第三方服务来告诉设备它在哪里。

于 2020-01-20T09:08:56.377 回答