2017-08-30 79 views
1

我已经通过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); 
    } 
} 
+0

'c.liveTrackConnectionMethod(“22”,“23”);'在ifelse之外工作吗? – Diamond

+0

它在里面工作... – beck

回答

1

我认为,一个应用程序将永远为isMinimized()时返回false该应用程序已关闭或最小化(即目前没有在前台运行)我可能是错误的。

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

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

+0

yeahh我做到了。我在System.out.println(“应用程序关闭时的地理围栏”)中的isMinimized()的其他部分。在调试时,应用程序关闭后我没有看到它。我也尝试过ConnectionRequest,但没有效果。 – beck

+0

“ConnectionRequest”脚本可能有问题。在答案中张贴您的摘录以提供清晰。删除敏感数据。 – Diamond

+0

嗨,上面有connectionRequest脚本的update1。它在应用程序关闭之前就起作用了(即,在我将它从后台移除之前也可以正常工作)。我在最小化方法中也有相同的连接代码 – beck

相关问题