Geofence does not work in Android devices
ognaranjo opened this issue · comments
The App set the Geofences once in the login process. Depending on how many Courts the Player selected. The following process set the fences:
` ///////////////////////////// GENERATE GEOFENCE PRIMARY COURT AND PREFERENCE COURTS /////////////
for (int i=0; i < UrlServ.WHOPLAYSCOURTS.getSize(); i++ ) {
boolean isLocation;
int courtDiameter;
CourtProfile courtProfile = (CourtProfile)UrlServ.WHOPLAYSCOURTS.getItemAt(i);
String fenceID = UserInfo.intUserName + "___" + courtProfile.getCourtId() + "___" + ((courtProfile.getCourtId() == userProfile.getPrimaryCourtID()) ? "1" : "0");
// + "___" + courtProfile.getCourtName() + "___" + userProfile.getNickName(); // Removed The Notification will be generated by the API
fenceID = Util.xorEncode(fenceID);
Location location = new Location();
location = courtProfile.getCourtPoint();
isLocation = !NonNull.empty(location.getLatitude());
courtDiameter = courtProfile.getCourtDiameter();
if (isLocation && courtDiameter > 0 ) {
Geofence gf = new Geofence(fenceID, location, courtDiameter, -1);
LocationManager.getLocationManager().addGeoFencing(GeofenceWhoPlaysImpl.class, gf);
}
}
`
The GeoFence Class:
`// GEOFENCE CLASS GeofenceWhoPlaysImpl
package com.example.app;
import com.codename1.io.ConnectionRequest;
import com.codename1.io.NetworkManager;
import com.codename1.location.GeofenceListener;
import com.codename1.ui.Display;
public class GeofenceWhoPlaysImpl implements GeofenceListener {
public static String API = "https://xxxx/xxxxx/";
@Override
public void onExit(String id) {
Display.getInstance().callSerially(() -> {
ConnectionRequest courtExitRequest = new ConnectionRequest();
courtExitRequest.setUrl(API + "xxxxx/xxxxxoutcourt/" + id);
courtExitRequest.setPost(false);
courtExitRequest.setFailSilently(true);
NetworkManager.getInstance().addToQueue(courtExitRequest);
});
}
@Override
public void onEntered(String id) {
Display.getInstance().callSerially(() -> {
ConnectionRequest courtExitRequest = new ConnectionRequest();
courtExitRequest.setUrl(API + "xxxxxx/xxxxxincourt/" + id);
courtExitRequest.setPost(false);
courtExitRequest.setFailSilently(true);
NetworkManager.getInstance().addToQueue(courtExitRequest);
});
}
}
`
I have the following hints in the app (codenameone_settings.properties)
codename1.arg.android.permission.ACCESS_BACKGROUND_LOCATION=true
codename1.arg.android.permission.ACCESS_FINE_LOCATION=true
It is working perfectly fine in iOs but it does not work at all in Android, Older, and new phones. I am currently testing on a phone with Android 11.
Note: in my testing, I create between 1 and 3 fences.
I pushed a small fix for geofencing on android yesterday that may be related. It will be available in the next update on friday.
Are background location events being received in your app?
Early this morning I updated Codename One to the new version 7.0.104. So far is working (BTW: I am very happy and excited). I feel like it behaves a little differently than iOS. I will keep testing to see if I should may little changes or not.
Thank you so much for supporting my project!