【问题标题】:ProximityAlert with a BroadcastReceiver not working带有 BroadcastReceiver 的 ProximityAlert 不起作用
【发布时间】:2012-08-16 20:07:32
【问题描述】:

我是 Android 新手,在使用 BroadcastReceiver 添加 ProximityAlert 时遇到问题。我知道这个话题之前也有人讨论过,但我正在尝试向不同的位置添加接近警报,我不确定我想要做的事情是否可以通过这种方式实现,或者我只是做错了。

问题:我已尝试使用 BroadcastReceiver 实现添加 ProximityAlert 的代码,但它无法正常工作。下面是我的代码中的 sn-p(发布在下面),请大家看看并帮助我。

我有这个 userLocations 列表。我通过为列表运行 for 循环将接近警报添加到所有用户提到的位置。如果用户之前没有访问过该特定位置,我只想向用户位置添加邻近警报。 然后我在 addLocationProximity() 方法中注册接收器,该方法是从 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);
                }
            }
        }
    }
}

BroadcastReceiver 的代码

    package com.android.locationmang;

public class LocationNotificationReceiver extends BroadcastReceiver {
    private static final int NOTIFICATION_ID = 1000;
    public static final String EVENT_ID_INTENT_EXTRA = "EventIDIntentExtraKey";

    @SuppressWarnings("deprecation")
    @Override
    public void onReceive(Context context, Intent intent) {
        String key = LocationManager.KEY_PROXIMITY_ENTERING;

        long eventID = intent.getLongExtra(EVENT_ID_INTENT_EXTRA, -1);

        Boolean entering = intent.getBooleanExtra(key, false);
        if (entering) {
            Log.d(getClass().getSimpleName(), "entering");
        }
        else{
            Log.d(getClass().getSimpleName(), "exiting");
        }

        String ns = Context.NOTIFICATION_SERVICE;

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
        Intent notificationIntent = new Intent(context, MarkAsCompleteActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        Notification notification = createNotification();
        notification.setLatestEventInfo(context, "Proximity Alert!", "You are near your point of interest.", pendingIntent);


        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }

    private Notification createNotification() {
        Notification notification = new Notification();
        notification.icon =  R.drawable.ic_launcher;
        notification.when = System.currentTimeMillis();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.ledARGB = Color.WHITE;
        notification.ledOnMS = 1500;
        notification.ledOffMS = 1500;
        return notification;
    }
}

感谢和问候

【问题讨论】:

    标签: notifications broadcastreceiver alert proximity


    【解决方案1】:

    您正在使用操作字符串创建广播Intent,而您的&lt;receiver&gt; 元素没有对应的&lt;intent-filter&gt;。变化:

    Intent intent = new Intent(PROX_ALERT_INTENT);
    

    到:

    Intent intent = new Intent(this, LocationNotificationReceiver.class);
    

    【讨论】:

    • 嗨@CommonsWare,我已经尝试进行更多更改,因为我想在用户靠近指定位置时删除ProximityAlert()。由于我对所有地址使用相同的 PendingIntent,因此很难识别我想要从获取接近警报中删除的特定 userLocation,因此在尝试添加接近警报时具有不同的意图对象。我这样做是对的吗?或者还有其他方法吗?此外,我正在使用 onLocationChanged(),并且在添加 addProximityAlert 之前就可以正常工作。两者一起使用有限制吗?
    • @user1593953:“我这样做是对的?或者还有其他方法吗?” - 我不知道。 “两者一起使用有限制吗?” -- 我不知道。
    • 嗨@CommonsWare,我已经以某种方式让它工作了......但现在警报不会停止......对于一个位置它只是继续n......是否有某个参数可以控制吗?
    • 嗨@CommonsWare,我仍然面临与addProximity()相同的问题......当应用程序没有运行时我无法让它工作......但是我决定去提供解决此问题的服务...在四处搜索时,我看到了 locationPoller,我猜它仅由您构建...如果可以,请举例说明我如何使用它来比较不同的位置addProximity 的?
    • @user1593953: LocationPoller 旨在“检查我们每小时的位置以检查 FourSquare”。它不是为接近检测而设计的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多