我的代码向ProximityIntentReceiver发送多个位置。我可以分开ID吗? 示例发送位置A B C D.位置A B C在输入时执行其他操作,但在输入位置D时进行通知。
私有类GeocoderTask扩展了AsyncTask> {
@Override
protected List<Address> doInBackground(LatLng... locations) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getContext());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(locations[i].latitude, locations[i].longitude, 5);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@SuppressLint("MissingPermission")
@Override
protected void onPostExecute(List<Address> addresses) {
//Log.i("Location","address = "+addresses);
if (addresses == null || addresses.size() == 0) {
Toast.makeText(getContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
if (addresses != null) {
Toast.makeText(getContext(), "Location found", Toast.LENGTH_SHORT).show();
for (int i = 0; i < addresses.size(); i++) {
Intent intent = new Intent("com.example.a57050358.testboat.util.proximityintentreceiver");
Address address = addresses.get(i);
latLng = new LatLng(address.getLatitude(), address.getLongitude());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(latLng);
circleOptions.radius(Rd);
PendingIntent proximityIntent = PendingIntent.getBroadcast(getContext(),i, intent, 0);
locationManager.addProximityAlert(address.getLatitude(), address.getLongitude(), Rd,i, proximityIntent);
}
IntentFilter filter = new IntentFilter ("com.example.a57050358.testboat.util.proximityintentreceiver");
getContext().registerReceiver(new ProximityIntentReceiver(), filter);
}
}
}
class ProximityIntentReceiver
public class ProximityIntentReceiver扩展BroadcastReceiver {
static int NOTIFICATION_ID = 1000 ;
int id = NOTIFICATION_ID;
boolean noti = false;
int short_gap = 200; // Length of Gap Between dots/dashes
int long_gap = 1000; // Length of Gap Between Words
long[] pattern = {
0, long_gap, short_gap, long_gap, short_gap, long_gap
};
private Vibrator vibrate;
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
String msg;
if (entering) {
Log.d(getClass().getSimpleName(), "entering");
msg = "Enter";
noti = true;
} else {
Log.d(getClass().getSimpleName(), "exiting");
msg = "Exit";
noti = true;
}
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
final Notification notification = createNotification();
notification.contentIntent = pendingIntent;
if (id == NOTIFICATION_ID) {
//action change station
AlertDialog.Builder builder = new AlertDialog.Builder(context, 2);
builder.setTitle(" Deaf Booster ");
builder.setMessage(" " + msg + " ");
builder.setIcon(R.drawable.icn3);
builder.setCancelable(false);
vibrate = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrate.vibrate(pattern, 0);
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
vibrate.cancel();
notification.flags = Notification.FLAG_AUTO_CANCEL;
}
});
builder.show();
}
id = NOTIFICATION_ID++;
}
private Notification createNotification() {
Notification notification = new Notification();
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_INSISTENT;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;
return notification;
}
}