从 GcmListenerService onMessageReceived 获取更新的位置

getting updated location from GcmListenerService onMessageReceived

你好,我正在开发一个应用程序,当它收到推送通知时应该获取它的位置并调用服务器,然后根据它显示通知或吞下它。

我遇到的问题是位置正在兑现,然后我没有及时通知我的客户,这是必不可少的。我显然无法订阅 onLocationChanged,因为我需要立即对消息做出决定。

我已经读过它,其中一个建议是初始化 com.google.android.gms.location.LocationListener,这应该会重置缓存,我认为它不会,但是启用 google 地图确实有助于获得更新的坐标所以我认为这确实是缓存问题。

目前代码如下(请不要判断它只是原型):

 private String getLocation() {
    // Get the location manager
    new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

        }
    };
    LocationManager locationManager = (LocationManager)
            getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    if (bestProvider != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return "-1.1329097,51.257538";
            }
        }
        Location location = locationManager.getLastKnownLocation(bestProvider);
        try {
            return location.getLatitude() + "," + location.getLongitude();
        } catch (NullPointerException e) {
            return "-1.1329097,51.257538";
        }
    }
    return "-1.1329097,51.257538";
}

 public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);
    sendNotification = true;
    callEmergency();
}

public boolean callEmergency(){

    StringBuffer chaine = new StringBuffer("");
    try{
        InstanceID instanceID = InstanceID.getInstance(this);
        String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        URL url = new URL("http://www.ears.uk.com/User/CheckForEmergency?token="+token+"&coordinates="+getLocation());
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        //  connection.setRequestProperty("User-Agent", "");
        connection.setRequestMethod("GET");
        //  connection.setDoInput(true);
        connection.connect();
        int code = connection.getResponseCode();
        InputStream inputStream = connection.getInputStream();

        BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        while ((line = rd.readLine()) != null) {
            chaine.append(line);
        }
        if(chaine.toString().equals("True")){
            sendNotification("Emergency Vehicle Approaching");
        }
    } catch (IOException e) {
        // writing exception to log
        e.printStackTrace();
    }

    return chaine.toString() == "True";
}

是否有更合适的解决方案?

如果没有任何东西触发位置更改,例如打开 google 地图或其他内容,您仍将拥有上次记录位置更改时的位置。

如果您需要可能的最新位置,您应该使用 getTime 检查该位置的时间,如果该位置的时间在您的限制范围内,那么您可以只使用该位置。如果那个时间是 5 小时,您可能想要获得一个新点,您必须在其中启动位置侦听器并获取第一个有效位置,然后停止侦听器。对于那部分,您应该启动一项服务,因为谁知道到达该位置需要多长时间