【问题标题】:ConnectionRequest when the app is in the background应用在后台时的 ConnectionRequest
【发布时间】: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


【解决方案1】:

我认为当应用程序关闭或最小化(即当前未在前台运行)时,应用程序将始终返回 falseisMinimized(),我对此可能是错误的。

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

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

【讨论】:

  • 是的,我做到了。我在 isMinimized() 的其他部分有 System.out.println("geofence when app is closed")。调试时,应用程序关闭后我看不到它。我在这里也试过 ConnectionRequest 但没有效果。
  • ConnectionRequest 脚本可能存在问题。在答案中发布您的 sn-p 以提供清晰性。删除敏感数据。
  • 嗨,上面有带有 connectionRequest 脚本的 update1。它在应用程序关闭之前工作(即在我从后台删除它之前工作)。我在最小化方法中也有相同的连接代码
  • @Diamond 是正确的,当应用程序关闭或最小化时 isMinimized() 将是正确的。如果该应用程序不在前台,isMinimized() 将返回 true。所以你这里的逻辑是倒退的。如果 isMinimized() 为真,您的“应用程序已关闭”情况应该是。我有一些应用程序在地理围栏处理程序中发出 ConnectionRequests,它们工作正常(应用程序在后台或被破坏)。
  • @stevehannah isMinimized() 在应用程序从最近的应用程序列表中滑出时不起作用。如果任何其他应用程序在前台,isMinimized 工作。我想知道在应用刷出后我们是否可以继续发送数据。
猜你喜欢
  • 2017-08-13
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
相关资源
最近更新 更多