我已经通过cn1测试了Geofence示例,它设置了本地通知。当应用程序关闭(被销毁)时,它仍然会发出通知。但我想通过GPS获取位置并运行connectionRequest以将它们保存在服务器中。我在下面的代码中替换了connectionRequest代码而不是LocalNotification,但它不起作用。我应该怎么做才能在应用程序关闭时运行connectionRequest(而不是在它被最小化但被销毁时),以便一旦用户安装并关闭(销毁它),应用程序就会在服务器中永远发送他/她的位置数据,直到应用程序被卸载。ConnectionRequest当应用程序在后台
Geofence gf = new Geofence(“test”,loc,100,100000); LocationManager.getLocationManager()。addGeoFencing(GeofenceListenerImpl.class,gf);
地理围栏localNotification:
public class GeofenceListenerImpl implements GeofenceListener {
@Override
public void onExit(String id) {
}
@Override
public void onEntered(String id) {
if(Display.getInstance().isMinimized()) {
Display.getInstance().callSerially(() -> {
Dialog.show("Welcome", "Thanks for arriving", "OK", null);
});
} else {
LocalNotification ln = new LocalNotification();
ln.setId("LnMessage");
ln.setAlertTitle("Welcome");
ln.setAlertBody("Thanks for arriving!");
Display.getInstance().scheduleLocalNotification(ln, 10, LocalNotification.REPEAT_NONE);
}
}
}
为什么下面不列入工作? (仅当应用程序正在运行或最小化而不是在它被销毁工作。)
public class GeofenceListenerImpl implements GeofenceListener {
@Override
public void onExit(String id) {
System.out.println("geofence onExit");
}
@Override
public void onEntered(String id) {
if(Display.getInstance().isMinimized()) {
Display.getInstance().callSerially(() -> {
System.out.println("geofence isMinimized");
});
} else {
System.out.println("geofence when app is closed");
//I want to run connectionRequest here but is not working
}
}
}
PS。我已经使用后台抓取,但它只适用于应用程序最小化。
UPDATE1:我是如何用最小connectionRequest()方法之外演示...
public class GeofenceListenerImpl implements GeofenceListener {
@Override
public void onExit(String id) {
System.out.println("geofence onExit");
}
@Override
public void onEntered(String id) {
if(Display.getInstance().isMinimized()) {
Display.getInstance().callSerially(() -> {
});
} else {
System.out.println("geofence when app is closed");
Connection c = new Connection();
c.liveTrackConnectionMethod("22" , "23");
}
}
}
Connection类
public class Connection {
ArrayList<Map<String, Object>> response;
public void liveTrackConnectionMethod(String lat, String lon) {
ConnectionRequest cr = new ConnectionRequest() {
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser jSONParser = new JSONParser();
Map parser = jSONParser.parseJSON(new InputStreamReader(input));
response = null;
}
};
cr.setPost(true);
cr.setUrl("http://url.com");
cr.addArgument("userid", Preferences.get(AllUrls.userIdPreference, null));
cr.addArgument("lat", lat + "");
cr.addArgument("long", lon + "");
cr.addRequestHeader("Accept", "application/json");
NetworkManager.getInstance().addToQueueAndWait(cr);
}
}
'c.liveTrackConnectionMethod(“22”,“23”);'在ifelse之外工作吗? – Diamond
它在里面工作... – beck