2015-04-02 53 views
0

所以,我試圖做一個尋寶遊戲的應用程序,並使用接近警報來顯示一些文本。我有很多問題,但我的第一個障礙是要弄清楚如何解決多個接近警報。在運行應用程序並通過DDMS發送GPS座標時,我沒有收到警報的通知。繼承人的代碼...如何在Android中結合多個近距離警報?

公共類FCRun延伸活動{

private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 19;    // in meters 
private static final long MINIMUM_TIME_BETWEEN_UPDATE = 10000;     // in Milliseconds 
private static final long POINT_RADIUS = 6;          // in meters 
private static final long PROX_ALERT_EXPIRATION = -1;       
private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY"; 
private static final String POINT_LONGITUDE_KEY = "POINT_LONGITUDE_KEY"; 

private static final String PROX_ALERT_INTENT = "King Hendo Tools"; 

private LocationManager lm; 
private MyLocationListener mylistener; 

private IntentFilter intFilter; 


@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fcrun); 

     // Get the location Manager (code from http://examples.javacodegeeks.com/android/core/location/android-location-based-services-example/) 
     lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     lm.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 
       MINIMUM_TIME_BETWEEN_UPDATE, 
       MINIMUM_DISTANCECHANGE_FOR_UPDATE, 
       new MyLocationListener()); 

     // the last known location of the provider 
     Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

     mylistener = new MyLocationListener(); 

     if (location != null) { 
      mylistener.onLocationChanged(location); 
     } else { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(intent); 
     } 
     // location updates: at least 15 meters and 10 seconds change 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 15, mylistener); 

} 

private class MyLocationListener implements LocationListener { 
    @Override 
    public void onLocationChanged(Location location) { 

    } 
    @Override 
    public void onProviderDisabled(String provider) { 

    } 
    @Override 
    public void onProviderEnabled(String provider) { 

    } 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 
} 

private void addProximityAlert(double latitude, double longitude) { 
    Intent intent = new Intent(PROX_ALERT_INTENT); 
    PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0); 

    /* 
    lm.addProximityAlert(
      latitude, // the latitude of the central point of the alert region 
      longitude, // the longitude of the central point of the alert region 
      POINT_RADIUS, // the radius of the central point of the alert region, in meters 
      PROX_ALERT_EXPIRATION, // 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 
      ); 
      */ 
    lm.addProximityAlert (38.19776, -84.8672, 6, 10000, proximityIntent); 
    lm.addProximityAlert (38.196, -84.8674, 6, 10000, proximityIntent); 
    lm.addProximityAlert (38.19518, -84.8666, 6, 10000, proximityIntent); 
    lm.addProximityAlert (38.19472, -84.8661, 6, 10000, proximityIntent); 
    lm.addProximityAlert (38.19388, -84.8649, 6, 10000, proximityIntent); 
    lm.addProximityAlert (38.19353, -84.8647, 6, 10000, proximityIntent); 
    lm.addProximityAlert (38.19373, -84.8661, 6, 10000, proximityIntent); 

    IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); 
    registerReceiver(new ProximityIntentReceiver(), filter); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 15, mylistener); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    lm.removeUpdates(mylistener); 
} 

}

繼承人的廣播接收器的代碼...

public class ProximityIntentReceiver extends BroadcastReceiver { 

private static final int NOTIFICATION_ID = 1000; 

@Override 
public void onReceive(Context context, Intent intent) { 
    String key = LocationManager.KEY_PROXIMITY_ENTERING; 
    Boolean entering = intent.getBooleanExtra(key, false); 
    if (entering) { 
     Log.d(getClass().getSimpleName(), "entering"); 

    } 
    else { 
     Log.d(getClass().getSimpleName(), "exiting"); 
    } 

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

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


    Notification notification = createNotification(); 
    // notification.setLatestEventInfo(context, "Proximity Alert", "You are near your point of interest.", pendingIntent); 
    notification.setLatestEventInfo(context, "Proximity Alert", "You are near your point of interest.", pendingIntent); 
    notificationManager.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_LIGHTS; 

    notification.ledARGB = Color.WHITE; 
    notification.ledOnMS = 1500; 
    notification.ledOffMS = 1500; 

    return notification; 
} 

}

任何想法,爲什麼我沒有得到通知彈出?

謝謝, 戴夫

+0

你實際上沒有調用addProximityAlerts() – 2015-04-02 19:41:49

+0

哪裏?這是否在getLastKnowLocation行之後? – 2015-04-02 20:12:52

+0

好的,我得到了它的工作。加布是正確的,一旦我想出瞭如何做到這一點,我的接近警報開始射擊。 – 2015-06-09 17:18:07

回答

0

好吧,我得到它的工作。加布是正確的,一旦我想出瞭如何做到這一點,我的接近警報開始射擊。

+0

請編輯此答案,以便它包含回答問題所需的所有詳細信息,而不僅僅是引用評論。 – josliber 2016-01-14 15:38:34