我是新來的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;
}
}
感謝和問候
嗨@CommonsWare,我都試過,因爲我想removeProximityAlert()一旦用戶附近的指定位置做一些更多的變化。由於我對所有地址使用相同的PendingIntent,所以很難確定要從接近警報中刪除的特定用戶位置,因此在嘗試合併接近警報時會有不同的意圖對象。我做對了嗎?還是有另一種方式呢? 另外,我正在使用onLocationChanged(),並且在添加addProximityAlert之前正常工作。是否有將這兩者結合使用的約束條件? – user1593953 2012-08-18 16:29:15
@ user1593953:「我做對了嗎?還是有另一種做法?」 - 我不知道。 「兩者在一起使用是否有限制?」 - 我並不知道。 – CommonsWare 2012-08-18 16:34:39
感謝您的幫助任何方式:)... – user1593953 2012-08-18 19:37:30