Notifications 广播接收器不工作时的ProximityAlert

Notifications 广播接收器不工作时的ProximityAlert,notifications,broadcastreceiver,alert,proximity,Notifications,Broadcastreceiver,Alert,Proximity,我是Android新手,在为BroadcastReceiver添加ProximityAlert时遇到问题。我知道这个话题之前也有人提到过,但我正在尝试在不同的地点添加近距离警示,我不确定我想做的是完全可以通过这种方式实现,还是我只是做错了 问题:我曾尝试实现将ProximityAlert添加到BroadcastReceiver的代码,但在某种程度上无法实现。下面是我的代码片段(贴在下面),请大家看一下并帮我解决 我有这个用户位置列表。我通过对列表运行for循环,向用户提到的所有位置添加接近警报。

我是Android新手,在为BroadcastReceiver添加ProximityAlert时遇到问题。我知道这个话题之前也有人提到过,但我正在尝试在不同的地点添加近距离警示,我不确定我想做的是完全可以通过这种方式实现,还是我只是做错了

问题:我曾尝试实现将ProximityAlert添加到BroadcastReceiver的代码,但在某种程度上无法实现。下面是我的代码片段(贴在下面),请大家看一下并帮我解决

我有这个用户位置列表。我通过对列表运行for循环,向用户提到的所有位置添加接近警报。我只想在用户之前未访问过的特定位置添加接近警报。 然后,我将接收器注册到addLocationProximition()方法中,该方法是从onResume()方法调用的。我在onPause()方法中注销接收器

我还使用onLocationChanged()方法根据添加接近警报时使用的相同逻辑填充了一个列表(稍后需要)

如果这些步骤中有任何一个没有正确执行,请务必告诉我

提前谢谢

    package com.android.locationmang;

public class ViewAActivity extends ListActivity implements LocationListener{

private static final String PROX_ALERT_INTENT = "com.android.locationmang.PROX_ALERT_INTENT";
private static final long LOCAL_FILTER_DISTANCE = 1200;
public static List<UserLocation> notifiedLocationsList;

public static Location latestLocation;
PendingIntent pendingIntent;
Intent notificationIntent;
private LocationManager locationManager;
List<UserLocations> userLocations;
private IntentFilter filter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        notifiedLocationsList = new ArrayList<UserLocation>();
        userLocations = getUserLocations(); //Returns a list of user Locations stored by the user on the DB

    filter = new IntentFilter(PROX_ALERT_INTENT);
    }

    private void setUpLocation() {
        locationNotificationReceiver = new LocationNotificationReceiver();

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60, 5, this);

        for (int i = 0; i < userLocation.size(); i++){
            UserLocation userLocation = userLocation.get(i); 
            if(!(userLocation.isComplete())){
                setProximityAlert(userLocation.getLatitude(), 
                        userLocation.getLongitude(), 
                        i+1, 
                        i);
            }
        }
        registerReceiver(locationNotificationReceiver, filter);
    }


    private void setProximityAlert(double lat, double lon, final long eventID, int requestCode){
            // Expiration is 10 Minutes (10mins * 60secs * 1000milliSecs)
            long expiration = 600000;

            Intent intent = new Intent(this, LocationNotificationReceiver.class);
            intent.putExtra(LocationNotificationReceiver.EVENT_ID_INTENT_EXTRA, eventID);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);

            locationManager.addProximityAlert(lat, lon, LOCAL_FILTER_DISTANCE, expiration, pendingIntent);
        }


    @Override
    protected void onResume() {
        super.onResume();

    setUpLocation();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60, 5, this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
        unregisterReceiver(locationNotificationReceiver);
    }

    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}


    public boolean userLocationIsWithinGeofence(UserLocation userLocation, Location latestLocation, long localFilterDistance) {
        float[] distanceArray = new float[1];
        Location.distanceBetween(userLocation.getLatitude(), userLocation.getLongitude(), latestLocation.getLatitude(), latestLocation.getLongitude(), userLocation.getAssignedDate(),distanceArray);

        return (distanceArray[0]<localFilterDistance);
    }

    public void onLocationChanged(Location location) {
        if (location != null) {
            latestLocation = location;

            for (UserLocation userLocation : userLocations) {
                if (!(userLocations.isVisited()) && userLocationIsWithinGeofence(userLocation, latestLocation, LOCAL_FILTER_DISTANCE)) {
                    notifiedLocationsList.add(userLocation);
                }
            }
        }
    }
}

感谢并问候

您正在使用操作字符串创建广播
意图
,而您的
元素没有相应的
。更改:

Intent intent = new Intent(PROX_ALERT_INTENT);
致:


您好@commonware,当用户靠近指定位置时,我想删除proximityalert(),因此我尝试进行了更多的更改。由于我对所有地址使用相同的PendingEvent,因此很难确定我要从获取接近警报中删除的特定用户位置,因此在尝试添加接近警报时,会有不同的目标对象。我这样做是对的?还是有别的方法?另外,我使用的是onLocationChanged(),这在添加addProximityAlert之前就已经起作用了。将两者结合使用是否有限制?@user1593953:“我这样做是对的?或者有其他方法吗?”——我不知道。“将两者结合使用有限制吗?”--我不知道。嗨@commonware,我不知怎么做到了。。。但是现在警报不会停止。。。对于一个位置,它只是在n上继续。。。是否有一个参数可以控制这一点?Hi@Commonware,我仍然面临与AddProximition()相同的问题。。。当应用程序未运行时,我无法使其工作。。。然而,我决定使用一种服务来解决这个问题。。。在四处搜索时,我看到了locationPoller n,我猜它是由你建造的。。。如果是,请举例说明我如何使用它来比较不同的位置,而不是添加邻近性?@user1593953:
LocationPoller
是为“每小时检查我们在哪里”而设计的。它不是为接近检测而设计的。
Intent intent = new Intent(PROX_ALERT_INTENT);
Intent intent = new Intent(this, LocationNotificationReceiver.class);