如何在 Android 中合并多个接近警报?
How do incorporate multiple proximity alerts in Android?
所以,我正在尝试制作一个寻宝游戏应用程序,并使用接近警报来显示一些文本。我有多个问题,但我的第一个障碍是弄清楚如何解决多个接近警报。当 运行 应用程序并通过 DDMS 发送 GPS 坐标时,我没有收到来自警报的通知。这是代码...
public class FCRun 扩展 Activity {
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;
}
}
关于为什么我没有收到弹出通知的任何想法???
谢谢,
戴夫
好的,我成功了。 Gabe 是正确的,一旦我弄清楚如何做到这一点,我的接近警报就开始触发。
所以,我正在尝试制作一个寻宝游戏应用程序,并使用接近警报来显示一些文本。我有多个问题,但我的第一个障碍是弄清楚如何解决多个接近警报。当 运行 应用程序并通过 DDMS 发送 GPS 坐标时,我没有收到来自警报的通知。这是代码...
public class FCRun 扩展 Activity {
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;
}
}
关于为什么我没有收到弹出通知的任何想法???
谢谢, 戴夫
好的,我成功了。 Gabe 是正确的,一旦我弄清楚如何做到这一点,我的接近警报就开始触发。