2012-08-16 215 views
0

我是新来的Android和有问题与一个BroadcastReceiver加入ProximityAlert。我知道这个话题早已被采纳,但我试图将接近警报添加到不同的位置,我不确定我想要做的事情是否可以通过这种方式实现,或者我只是做错了。ProximityAlert与广播接收器不工作

问题:我试图实现的代码与一个BroadcastReceiver加入ProximityAlert,但它不工作的一些方式。下面是我的代码片段(发布如下),请求所有人请看看并帮助我。

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

规范广播接收器

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

感谢和问候

回答

0

您正在创建一个广播Intent与动作字符串,你<receiver>元素没有相应的<intent-filter>。变化:

Intent intent = new Intent(PROX_ALERT_INTENT); 

到:

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

嗨@CommonsWare,我都试过,因为我想removeProximityAlert()一旦用户附近的指定位置做一些更多的变化。由于我对所有地址使用相同的PendingIntent,所以很难确定要从接近警报中删除的特定用户位置,因此在尝试合并接近警报时会有不同的意图对象。我做对了吗?还是有另一种方式呢? 另外,我正在使用onLocationChanged(),并且在添加addProximityAlert之前正常工作。是否有将这两者结合使用的约束条件? – user1593953 2012-08-18 16:29:15

+0

@ user1593953:“我做对了吗?还是有另一种做法?” - 我不知道。 “两者在一起使用是否有限制?” - 我并不知道。 – CommonsWare 2012-08-18 16:34:39

+0

感谢您的帮助任何方式:)... – user1593953 2012-08-18 19:37:30