2

我創建了一些ProximityAlerts。這似乎只是第一件作品,第二件作品。他們似乎也過期了。我把這種方法來創建ProximityAlert:addProximityAlert最初的作品

public boolean addProximityAlert(int id) { 

    locationManager.addProximityAlert(
    latitude, 
    longitude, 
    POINT_RADIUS,    
    PROX_ALERT_EXPIRATION,  
    getPendingIntent(id)  
); 

    registerReceiver(new ProximityIntentReceiver(), getIntentFilter(id)); 

    return true; 
} 

private PendingIntent getPendingIntent(int id) { 
    Intent intent = new Intent(PROX_ALERT_INTENT + id); 
    intent.setAction(String.valueOf(id)); 
    return PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
} 

private IntentFilter getIntentFilter(int id) { 
    IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT + id); 
    filter.addAction(String.valueOf(id)); 
    return filter; 
} 

在ProximityIntentReceiver擴展廣播接收器:

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

    Boolean entering = intent.getBooleanExtra(key, false); 

    if (entering) { 
     message = context.getString(R.string.notification_alert_entering); 
    } else { 
     message = context.getString(R.string.notification_alert_exiting); 
    } 

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

    Notification notification = createNotification(); 
    notification.setLatestEventInfo(context, context.getString(R.string.notification_alert), message, pendingIntent); 

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(NOTIFICATION_ID, notification); 

} 

變量:

private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1; // in Meters 
private static final long MINIMUM_TIME_BETWEEN_UPDATE = 1000;  // in Milliseconds 
private static final long POINT_RADIUS = 500;      // in Meters 
private static final long PROX_ALERT_EXPIRATION = -1; 
private static final String PROX_ALERT_INTENT = "com.xxx.yyy.ProximityAlert"; 

我想創造多項獨有的警報,然後每次進入或退出鄰近時都會收到通知(可能會發生多次)。所有的警報應該保持有效,有人可以請我指出爲什麼這個工作不正常嗎?

+0

你怎麼測試?您是否檢查是否將進入/退出POINT_RADIUS進入實際經/多頭位置?警報只會在穿越POINT_RADIUS時出現。 – Shrikant

+0

@Shrikant更新後的問題。我使用真實設備進行測試,並從該位置移入和移出。 IE瀏覽器。創建一個警報,駕駛我的車2公里,然後開車回位置。 – powder366

+0

你是否也有很好的互聯網連接?因爲你的代碼對我來說很好看。我也做了同樣的方式,它運作得很好(我幾乎準確的位置得到警報)。嘗試減少MINIMUM_TIME_BETWEEN_UPDATE(用於測試將其設置爲0) – Shrikant

回答

1

這是我如何添加接近:

String geo = "geo:" + storeProximityData.getLatitude() + "," 
        + storeProximityData.getLongitude(); 
      Intent intent1 = new Intent(PROXIMITY_ALERT_HOME, Uri.parse(geo)); 
      PendingIntent proximityIntent = PendingIntent.getBroadcast(
        sContext.getApplicationContext(), 0, intent1, 
        PendingIntent.FLAG_CANCEL_CURRENT); 
      sLocationManager.addProximityAlert(
        storeProximityData.getLatitude(), // the 
        // latitude 
        // of 
        // the 
        // central 
        // point 
        // of 
        // the alert region 
        storeProximityData.getLongitude(), // the longitude of 
                 // the 
                 // central point of 
                 // the 
                 // alert 
        // region 
        storeProximityData.getRadius(), // the radius of the 
                // central 
                // point of the 
                // alert 
                // region, in 
        // meters 
        expirationTime, // time for this proximity alert, in 
        // milliseconds, or -1 to 
        // indicate no expiration 
        proximityIntent // will be used to generate an Intent to 
            // fire 
        // when entry to or exit from the alert region 
        // is detected 
        ); 

,這是我如何註冊接收器:

private static String PROXIMITY_ALERT_HOME = "path.to.ProximityAlert"; 
IntentFilter filter = new IntentFilter(PROXIMITY_ALERT_HOME); 
      filter.addDataScheme("geo"); 
      sContext.registerReceiver(
        new ProximityResponderBroadcastReceiver(), filter); 
+0

不客氣! – Shrikant

+0

看來addProximityAlert是不可靠的。不知道我的代碼是否錯誤? – powder366

+0

這段代碼幾乎適用於所有的時間。但文件也表示,它幾乎不是每次都有效。可能還有其他一些問題。文檔說:由於位置估算的近似性質,如果設備短暫通過給定區域,可能不會發射Intent。同樣,如果設備經過非常靠近給定區域但實際上並未進入,則可以觸發意圖。禮貌:http://developer.android.com/reference/android/location/LocationManager.html#addProximityAlert(double,double,float,long,android.app.PendingIntent) – Shrikant