从一个活动将数据发送到另一个定期 [英] Send data from one activity to another regularly

查看:212
本文介绍了从一个活动将数据发送到另一个定期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定期熔位置提供每个30秒得到我的主要活动场所。我不使用的服务或其他任何东西。我得到的位置,就当用户在应用程序。

I get my location in Main Activity regularly by Fused Location Provider each 30 seconds. I don't use service or anything else. i get location just when user is in application.

在主要活动,我有一个按钮,当pressed,转到第二个活动,并显示我在地图上的当前位置(通过这条线:googleMap.setMyLocationEnabled(真))。

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)).

1 - 我想从主要活动定期发送我的位置数据,第二个活动(每个30秒调用onLocationChange法),动我的当前位置的标记。我怎么能定时发送位置信息?并在我把codeS的吗?在onChangedLocation方法? (或它不是必要的呢?并当它改变了我的位置自动移动?)

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. How can i send location data regularly? and where i put codes for this? in onChangedLocation method? (Or it's not necessary at all? and my location move automatically when it's changed?)

另外,我送我的当前位置到服务器,在OnLocationChanged方法,用排枪库是这样的:

Also, i send my current location to server, in OnLocationChanged method, by Volley library like this:

@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 - 当我们在另一个活动是否onLocationChanged方法的工作原理,甚至

2- Does onLocationChanged method works even when we are in another activities?

3 - 如果有,请问AlertDialog可以在每一个活动说明了什么? (不仅在主要活动是showAlertDialog方法和LocationListener的在那里)。

3- If yes, Does AlertDialog can show in every activities? (not only in Main Activity that showAlertDialog method and LocationListener are there).

4 - 有没有更好的方式来处理响应,并做相关的东西。

4- Is there better way to handle responses and do related stuffs?

推荐答案

只有一个活动的活动,并在任何时间运行。你可能会更好用2片段,或在你的MainActivity只是一个额外的图形页面,你可以做,而不是具有不同的活动可见还是走了。

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.

或者你使用的服务,它可以在后台并行运行,以您的任何活动。

Or you use a service, which can run in the background in parallel to any of your Activities.

这篇关于从一个活动将数据发送到另一个定期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆