从GcmListenerService onMessageReceived获取更新的位置

时间:2016-03-15 20:35:42

标签: android push-notification gps geolocation google-geolocation

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

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

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

目前代码看起来像这样(请不要只判断它的原型):

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

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

1 个答案:

答案 0 :(得分:1)

如果没有触发位置更改,即使打开谷歌地图或其他东西,您仍将拥有上次记录位置更改时的位置。

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