用户靠近位置时的通知

时间:2013-09-18 15:12:00

标签: android locationlistener

在我的应用程序中,我有一个带有关联lat长位置的第9轮位置列表,最终目标是通知用户它们是否在任何位置的半英里范围内。

最好的方法是什么?这是我到目前为止所做的。

distanceFromListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            List<JSONObject> centres = sortVenuesByNearest(location);
            Log.d("FUApp", "distanceFromListener setting location");
            haveLocation = true;
            theLocation = location;

            float accuracy = location.getAccuracy();
            for (JSONObject centre : centres) {
                try {
                    Double distance = Double.valueOf(centre.getString("distanceTo"));
                    String name = centre.getString("name");
                    Integer venueID = Integer.valueOf(centre.getString("id"));
                    if (distance < 804.672) {
                        Bundle mBundle = new Bundle();
                        Double miles = (distance * 0.00062137);
                        DecimalFormat df = new DecimalFormat("#.##");
                        String miles2 = df.format(miles);
                        mBundle.putString(ExtraKeyCentreName, name);
                        mBundle.putString(ExtraKeyCentreDist, miles2 + " miles away");
                        mBundle.putString(ExtraKeyCentreJSON, centre.toString());
                        mBundle.putString(ExtraKeyCentrePostcode, "none");

                        sendNotification(ActivityCentrePage.class, "Visit " + name, "You're half a mile from " + name + ". Why not come for a visit?", R.drawable.ic_launcher, venueID, mBundle);
                    } else {
                        cancelNotification(venueID);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };

    Boolean notify_if_close = sp.getBoolean(SettingsNotifyIfClose, true);
    if (notify_if_close) {
        locationService.requestLocationUpdates(LocationManager.GPS_PROVIDER, 50, 100, distanceFromListener);
    }

这样可行,但是如果用户取消通知说关闭,当下次更新位置时,它会再次弹出,这会很烦人。请问一个合适的替代方案是一个很大的minTime,比如说requestLocationUpdates上半小时?

2 个答案:

答案 0 :(得分:7)

答案 1 :(得分:1)

使用服务跟踪坐标...

试试这个链接,它可能对你有帮助..

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/