Codenameone 应用程序处于后台时的ConnectionRequest

Codenameone 应用程序处于后台时的ConnectionRequest,codenameone,Codenameone,我已经通过cn1测试了Geofence示例,其中它设置了本地通知。当应用程序关闭(被销毁)时,它仍会发出通知。但我想通过GPS获取位置并运行connectionRequest将它们保存在服务器中。我在下面的代码中替换了connectionRequest代码而不是LocalNotification,但它不起作用。当应用程序关闭(不是最小化而是销毁)时,我应该如何运行connectionRequest,以便一旦用户安装并关闭(销毁)应用程序,应用程序将永远在服务器中发送他/她的位置数据,直到卸载应用

我已经通过cn1测试了Geofence示例,其中它设置了本地通知。当应用程序关闭(被销毁)时,它仍会发出通知。但我想通过GPS获取位置并运行connectionRequest将它们保存在服务器中。我在下面的代码中替换了connectionRequest代码而不是LocalNotification,但它不起作用。当应用程序关闭(不是最小化而是销毁)时,我应该如何运行connectionRequest,以便一旦用户安装并关闭(销毁)应用程序,应用程序将永远在服务器中发送他/她的位置数据,直到卸载应用程序为止

土工围栏gf=新土工围栏(“测试”,loc,100100000); LocationManager.getLocationManager().AddGeoFerning(GeofenceListenerImpl.class,gf)

带有本地通知的地理围栏:

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);
        }
    }    
}
为什么下面的方法不起作用?(它仅在应用程序正在运行或最小化时起作用,但在应用程序被销毁时不起作用。)

另外,我已经使用了后台提取,但它只在应用程序最小化时才起作用

更新1:演示如何在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);
    }
}
公共类连接{
阵列响应;
public void liveTrackConnectionMethod(字符串lat、字符串lon){
ConnectionRequest cr=新的ConnectionRequest(){
@凌驾
受保护的void readResponse(InputStream输入)引发IOException{
JSONParser JSONParser=新的JSONParser();
Map parser=jSONParser.parseJSON(新的InputStreamReader(输入));
响应=空;
}
};
cr.setPost(真);
cr.setUrl(“http://url.com");
cr.addArgument(“userid”,Preferences.get(AllUrls.userIdPreference,null));
cr.addArgument(“lat”,lat+”);
cr.addArgument(“long”,lon+”);
cr.addRequestHeader(“接受”、“应用程序/json”);
NetworkManager.getInstance().addToQueueAndWait(cr);
}
}

我认为当应用程序关闭或最小化(即当前未在前台运行)时,对于
isMinimized()
,应用程序将始终返回
false
,我可能对此有误

尝试在
isMinimized()之外调用
connectionRequest
脚本。毕竟,无论用户是否在使用该应用程序,您都希望跟踪用户的位置


使用
LocalNotification
的第一个解决方案将在用户使用应用程序时通过调用
else
部分而不是
对话框来向用户显示通知,因为
isMinimized()
将为
false

是的。在isMinimized()的其他部分中,我有System.out.println(“应用程序关闭时的地理围栏”)。调试时,我在应用程序关闭后看不到它。我在这里也尝试了ConnectionRequest,但没有效果。
ConnectionRequest
脚本可能有问题。在答案中张贴您的片段,以提供清晰性。删除敏感数据。您好,上面有带有connectionRequest脚本的update1。它在应用程序关闭之前工作(即在我将其从后台删除之前也工作)。我在最小化方法中的连接代码与well@Diamond当应用程序关闭或最小化时,isMinimized()将为true,这是正确的。如果该应用程序不在前台,isMinimized()将返回true。所以你的逻辑是反向的。如果isMinimized()为true,则“应用程序已关闭”的大小写应为。我有几个应用程序在geofence处理程序中发出连接请求,它们工作正常(应用程序处于后台或已销毁)。@stevehannah isMinimized()在通过从最近的应用程序列表中刷出关闭应用程序时不工作。如果任何其他应用程序在前台,isMinimized都可以工作。我想知道我们是否可以在应用程序被刷出后继续发送数据在ifelse外部工作?如果。。。
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);
    }
}