【发布时间】:2017-08-30 15:44:18
【问题描述】:
我已经通过 cn1 测试了设置本地通知的地理围栏示例。当应用程序关闭(被销毁)时,它仍然会发出通知。但我想通过 GPS 获取位置并运行 connectionRequest 将它们保存在服务器中。我在以下代码中替换了 connectionRequest 代码而不是 LocalNotification,但它不起作用。当应用程序关闭时(不是最小化而是销毁),我应该怎么做才能运行连接请求,这样一旦用户安装并关闭(销毁)它,应用程序就会永远在服务器中发送他/她的位置数据,直到应用程序已卸载。
地理围栏 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:演示我如何在minimized() 方法之外使用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");
}
}
}
连接类
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 之外工作吗? -
它在内部工作,如果...
标签: codenameone