我正在开发模块,其中驾驶员等人处于移动状态并定期在时间段上发送他的位置,其在地图上有航点和方向,我想知道驾驶员是否已进入特定航点区域?我发现Proximity-Alert如果用户进入特定区域就会发出警报,但我想了解其他用户,例如司机,那么我怎么知道司机是否接近特定的路点?我的Proximity-Alert代码如下。当司机进入区域或我必须使用别的东西时,我能用它来发出警报吗?
public class ProximityIntentReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
@SuppressWarnings("deprecation")
@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);
Intent notificationIntent = new Intent(context, ProximityAlertActivity.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);
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;
}
}
ProximityAlertActivity.java
public class ProximityAlertActivity extends Activity {
private static final long POINT_RADIUS = 100; // in Meters
private static final long PROX_ALERT_EXPIRATION = -1; // It will never expire
private static final String PROX_ALERT_INTENT = "com.androidmyway.demo.ProximityAlert";
private LocationManager locationManager;
private EditText latitudeEditText;
private EditText longitudeEditText;
private Button addAlertButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_proxymity);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
latitudeEditText = (EditText) findViewById(R.id.point_latitude);
longitudeEditText = (EditText) findViewById(R.id.point_longitude);
addAlertButton = (Button) findViewById(R.id.add_alert_button);
addAlertButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addProximityAlert();
}
});
}
private void addProximityAlert() {
double latitude = Double.parseDouble(latitudeEditText.getText().toString());
double longitude = Double.parseDouble(longitudeEditText.getText().toString());
Intent intent = new Intent(PROX_ALERT_INTENT);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
locationManager.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
);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
registerReceiver(new ProximityIntentReceiver(), filter);
Toast.makeText(getApplicationContext(),"Alert Added",Toast.LENGTH_SHORT).show();
}
}