与广播接收器不工作ProximityAlert

人气:434 发布:2022-09-17 标签: notifications alert broadcastreceiver proximity

问题描述

我是新来的Andr​​oid和有问题与一个BroadcastReceiver加入ProximityAlert。我知道,这个话题已经采取了较早的很好,但我试图接近警戒添加到不同的位置,我不知道什么,我试图做的是相当实现这样或我只是做是错误的。

I am new to Android and have a problem with adding ProximityAlert with a BroadcastReceiver. I know that this topic has been taken up earlier as well but I am trying to add proximity alert to different locations and I am not sure if what I am trying to do is quite achievable this way or I am just doing it wrong.

问题:我试图实现code与一个BroadcastReceiver加入ProximityAlert,但它不工作一些如何。下面是从我的code,要求大家请看看,并帮助我与它的代码段(贴在下面)。

Problem : I have tried to implement the code for adding ProximityAlert with a BroadcastReceiver, but its not working some how. Below is the snippet from my code (posted below) requesting all to please have a look and help me out with it.

我有这个userLocations名单。我通过运行一个for循环的列表中添加接近警报到所有的用户提到的位置。我只是想,如果那个特定的位置并没有被用户访问过,提醒邻近添加到用户的位置。然后我登记在addLocationProximity()方法,它是从onResume()方法被称为接收器。我unregisterReceiver接收器中的onPause()方法。

I have this userLocations list. I am adding Proximity Alert to all the user mentioned location by running a for loop for the list. I only want to add a proximity Alert to the user location if that particular location has not been visited by the user before. I then register the receiver in the addLocationProximity() method, which is called from the onResume() method. I unregisterReceiver the receiver in the onPause() method.

我也用了onLocationChanged()方法来填充列表在此基础上已被用于添加接近警报相同的逻辑(我会为需要更高版本)。

I have also used the onLocationChanged() method to populate a list (which I would be needing for later) based on the same logic which have been used to add the proximity alert.

请不要让我知道如果这些步骤都没有被正确执行。

Please do let me know if any of these steps have not been carried out correctly.

先谢谢了。

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

code为广播接收器

Code for 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;
    }
}

感谢和问候

推荐答案

您正在创建一个广播意图与动作字符串,你的&LT ;接收器GT; 元素不具备相应的&LT;意向滤光器&gt; 。变化:

You are creating a broadcast Intent with an action string, and your <receiver> element does not have the corresponding <intent-filter>. Change:

Intent intent = new Intent(PROX_ALERT_INTENT);

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

263