我正在做一个Android应用程序,我希望在用户到达目的地时生成警报/调用。我已经在不同的距离测试了我的代码,结果是仅在短距离内发出警报/呼叫。
这里是我的代码:任何人都可以告诉我应该改变什么?
MainActivity.java
Geofence geofence = new Geofence.Builder()
.setCircularRegion(latlng1.latitude, latlng1.longitude, MyRadius)
.setRequestId(result)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
myFences.add(geofence);
googleApiClient = new GoogleApiClient.Builder(GpsAlarmMapsActivity.this)
.addApi(LocationServices.API)
.addConnectionCallbacks(GpsAlarmMapsActivity.this)
.addOnConnectionFailedListener(GpsAlarmMapsActivity.this)
.build();
googleApiClient.connect();
GeofenceTransitionReceiver.java
public class GeofenceTransitionReceiver extends WakefulBroadcastReceiver {
int active_id = 0;
public static final String TAG = GeofenceTransitionReceiver.class.getSimpleName();
private Context context;
String code;
public static String Location_status;
Uri tone;
private DBHelper mydb1;
public GeofenceTransitionReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "onReceive(context, intent)");
this.context = context;
code = GpsAlarmMapsActivity.note;
Log.i("Code....................", "code : " + code);
GeofencingEvent event = GeofencingEvent.fromIntent(intent);
if (event != null) {
if (event.hasError()) {
onError(event.getErrorCode());
} else {
int transition = event.getGeofenceTransition();
if (transition == Geofence.GEOFENCE_TRANSITION_ENTER || transition == Geofence.GEOFENCE_TRANSITION_DWELL || transition == Geofence.GEOFENCE_TRANSITION_EXIT) {
String[] geofenceIds = new String[event.getTriggeringGeofences().size()];
for (int index = 0; index < event.getTriggeringGeofences().size(); index++) {
geofenceIds[index] = event.getTriggeringGeofences().get(index).getRequestId();
}
if (transition == Geofence.GEOFENCE_TRANSITION_ENTER || transition == Geofence.GEOFENCE_TRANSITION_DWELL) {
onEnteredGeofences(geofenceIds);
event=null;
} else {
onExitedGeofences(geofenceIds);
}
}
}
}
}
protected void onEnteredGeofences(String[] geofenceIds) {
for (String fenceId : geofenceIds) {
// Toast.makeText(context, String.format("Entered this fence: %1$s", fenceId), Toast.LENGTH_SHORT).show();
Log.i(TAG, String.format("Entered this fence: %1$s", fenceId));
mydb1 = new DBHelper(context.getApplicationContext());
Location_status = "Entered";
Log.i("on entered : ", "status " + Location_status);
createNotification(fenceId, "Entered");
}
}
protected void onExitedGeofences(String[] geofenceIds) {
for (String fenceId : geofenceIds) {
// Toast.makeText(context, String.format("Exited this fence: %1$s", fenceId), Toast.LENGTH_SHORT).show();
Log.i(TAG, String.format("Exited this fence: %1$s", fenceId));
// createNotification(fenceId, "Exited");
}
}
protected void onError(int errorCode) {
// Toast.makeText(context, String.format("onError(%1$d)", errorCode), Toast.LENGTH_SHORT).show();
Log.e(TAG, String.format("onError(%1$d)", errorCode));
}
private void createNotification(String fenceId, String fenceState) {
if (code == "alarm")
tone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
else
tone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Intent notificationIntent = new Intent(context, StatPendingActivity.class);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setAction(Intent.ACTION_MAIN);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setAutoCancel(true);
notificationBuilder
.setContentText("Title")
.setSound(tone)
.setContentTitle("Reached ! ")
.setSmallIcon(R.drawable.ic_stat_action_room)
.addAction(R.drawable.ic_stat_action_room, "Cancel", pendingIntent)
.setColor(Color.argb(0x55, 0x00, 0x00, 0xff))
.setTicker(String.format("%1$s Destination: %2$s", fenceState, fenceId));
notificationBuilder.setContentIntent(pendingIntent);
notificationManager.notify(R.id.notification, notificationBuilder.build());
}
}