Android更新文本视图计时器和刷新地图内容?
我有下面的布局文件。基本上我有谷歌地图,在左上角我有一个Android更新文本视图计时器和刷新地图内容?,android,google-maps,timer,Android,Google Maps,Timer,我有下面的布局文件。基本上我有谷歌地图,在左上角我有一个TextView,我需要每隔15秒刷新一次地图。布局很好 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heigh
TextView
,我需要每隔15秒刷新一次地图。布局很好
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.example.ns.appversion1.MapActivityFragment1"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:padding="3dp"
android:id="@+id/textRefresh"
android:background="@drawable/border"
android:text="Refreshing 1"
android:textColor="#03A9FA"
android:textSize="12sp" />
</LinearLayout>
</FrameLayout>
</RelativeLayout>
我试过Charu的解决方案如下
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map_activity_fragment1, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
tv = (TextView) getActivity().findViewById(R.id.textRefresh);
MyCount counter = new MyCount(15000, 1000);
counter.start();
return view;
}
这将演示从15倒计时到0(-1秒),并且每15秒将再次重复此任务
public class YourActivity extends Activity {
TextView textView;
android.os.Handler customHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
textView = (TextView) findViewById(R.id.text);
customHandler = new android.os.Handler();
customHandler.postDelayed(updateTimerThread, 0);
}
// count down timer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
textView.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
textView.setText("Left: " + millisUntilFinished / 1000);
}
}
private Runnable updateTimerThread = new Runnable()
{
public void run()
{
// 10000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(15000, 1000);
counter.start();
customHandler.postDelayed(this, 15000); // repeat body after 15 seconds
}
};
}
只是一个建议
据我所知,处理程序在主线程上运行,所以,一旦该线程负责所有UI交互,请尝试使用轻处理任务。如果您需要处理大量数据或需要一些时间,比如等待服务器的响应,我建议您使用另一个线程,比如使用异步任务或协同路由
就我个人而言,我喜欢两者兼用。我使用处理程序来创建此事件,一旦到了创建时间,我就会调用在另一个线程中运行的函数来执行此操作。您说了很多。你到底想做什么;)1.在15秒内刷新地图,或者使用倒计时?我只想在我的标签上显示倒计时,从15、14、13等一直到1,然后我调用json刷新地图?注释不用于扩展讨论;这段对话已经结束。
public class YourActivity extends Activity {
TextView textView;
android.os.Handler customHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
textView = (TextView) findViewById(R.id.text);
customHandler = new android.os.Handler();
customHandler.postDelayed(updateTimerThread, 0);
}
// count down timer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
textView.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
textView.setText("Left: " + millisUntilFinished / 1000);
}
}
private Runnable updateTimerThread = new Runnable()
{
public void run()
{
// 10000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(15000, 1000);
counter.start();
customHandler.postDelayed(this, 15000); // repeat body after 15 seconds
}
};
}