简体   繁体   English

定期将数据从一项活动发送到另一项活动

[英]Send data from one activity to another regularly

I get my location in Main Activity regularly by Fused Location Provider each 30 seconds. 我每30秒就会通过融合定位服务提供商定期在主要活动中获取自己的位置。 I don't use service or anything else. 我不使用服务或其他任何服务。 i get location just when user is in application. 我只是在用户在应用程序中时获取位置。

In Main Activity i have a button that when pressed, goes to the Second Activity and show my current location on the map (By this line: googleMap.setMyLocationEnabled(true)). 在“主要活动”中,我有一个按钮,按下该按钮时,进入“第二个活动”并在地图上显示我当前的位置(此行:googleMap.setMyLocationEnabled(true))。

1- I want to send my location data from Main Activity to Second Activity regularly(each 30 second that onLocationChange method called), to move my current location's marker. 1-我想定期将自己的位置数据从“主要活动”发送到“第二个活动”(每30秒调用onLocationChange方法),以移动我当前位置的标记。 How can i send location data regularly? 如何定期发送位置数据? and where i put codes for this? 我在哪里放置代码呢? in onChangedLocation method? 在onChangedLocation方法? ( Or it's not necessary at all? and my location move automatically when it's changed?) 还是根本没有必要?更改后我的位置会自动移动吗?)

Also, i send my current location to server, in OnLocationChanged method, by Volley library like this: 另外,我通过Volley库以OnLocationChanged方法将当前位置发送到服务器,如下所示:

@Override
public void onLocatuionChanged(Location location){
if(location != null)
    sendLocation(location);
}

private void sendLocation(Location location){
Map<String, String> params = new HashMap<String, String>();
params.put("latitude", String.valueOf(location.getLatitude();
params.put("longitude", String.valueOf(location.getLongitude();
jsonObjReq = new VolleyCustomReq(Method.POST,
URL, params,
new Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response){
if(response != null)
    if(response.optString("type").equalsIgnoreCase("1")
        showAlertDialog1();
    else if(response.optString("type").equalsIgnoreCase("2")
        showAlertDialog2();
    else if(response.optString("type").equalsIgnoreCase("3")
        showAlertDialog3();
    else if(response.optString("type").equalsIgnoreCase("addNewMarker")
        addNewMarkerOnMapAsDestinationAndMakePathBetweenBothOfYou();
    ...
}
...
}
}

If there is a response from server, depending on the response type, i want to do something; 如果服务器响应,则根据响应类型,我想做一些事情; for example show related message to user, add a marker as destination on the map and make path between two markers. 例如向用户显示相关消息,在地图上添加标记作为目的地,并在两个标记之间建立路径。

2- Does onLocationChanged method works even when we are in another activities? 2-即使我们处于其他活动中,onLocationChanged方法也可以使用吗?

3- If yes, Does AlertDialog can show in every activities? 3-如果是,AlertDialog是否可以在每个活动中显示? (not only in Main Activity that showAlertDialog method and LocationListener are there). (不仅在Main Activity中,还有showAlertDialog方法和LocationListener)。

4- Is there better way to handle responses and do related stuffs? 4-有没有更好的方法来处理回应和做相关的事情?

There's only one activity active and running at any time. 任何时候都只有一个活动处于活动状态并正在运行。 You might be better off with 2 fragments or just an additional MapView in your MainActivity which you can make visible or gone, instead of having different activities. 在MainActivity中使用2个片段或仅一个附加MapView可能会更好,您可以使其可见或消失,而不必进行其他活动。

Or you use a service, which can run in the background in parallel to any of your Activities. 或者,您使用一项服务,该服务可以在后台与任何“活动”并行运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM