Java android中周期任务的实现
我想创建一个小的android应用程序,在点击按钮(即设置活动)后,以周期性的间隔显示系统时间。通过Intent创建按钮和设置周期性活动的代码如下:Java android中周期任务的实现,java,android,timer,android-activity,timertask,Java,Android,Timer,Android Activity,Timertask,我想创建一个小的android应用程序,在点击按钮(即设置活动)后,以周期性的间隔显示系统时间。通过Intent创建按钮和设置周期性活动的代码如下: package com.example.timeupdate; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; impo
package com.example.timeupdate;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
TextView show;
@Override
protected void onCreate(Bundle I_Love_Biriyani) {
super.onCreate(I_Love_Biriyani);
setContentView(R.layout.activity_main);
button = (Button) findViewById (R.id.pressButton);
show = (TextView) findViewById (R.id.Show);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent openTimeUpdater = new Intent("com.example.timeupdate.TIMEUPDATER");
startActivity(openTimeUpdater);
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
下面是我使用TimerTask类执行任务时重复计时器(比如5秒)的代码:
package com.example.timeupdate;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TimeUpdater extends Activity {
TextView Show;
TimerTask timer= new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
Date d = new Date();
Show.setText(""+d);
}
};
@Override
protected void onCreate(Bundle hotovaga) throws IllegalStateException {
// TODO Auto-generated method stub
super.onCreate(hotovaga);
setContentView(R.layout.new_update);
Show = (TextView) findViewById (R.id.time);
Timer t = new Timer();
t.scheduleAtFixedRate(timer , 0 , 5000);
}
}
单击按钮后,时间只显示一次,然后应用程序停止显示对话框消息。需要解释才能以同样的方式完成此工作。您正在尝试访问非UI线程中的UI元素
Show.setText(""+d);
相反,将其包装在runOnUiThread
接口中以获得正确的输出
为您的TimeUpdater
类使用以下代码
public class TimeUpdater extends Activity {
TextView Show = null;
Calendar c;
int seconds;
int minutes;
int hours;
TimerTask timer= new TimerTask(){
@Override
public void run() {
c = Calendar.getInstance();
seconds = c.get(Calendar.SECOND);
minutes = c.get(Calendar.MINUTE);
hours = c.get(Calendar.HOUR);
runOnUiThread(new Runnable() {
@Override
public void run() {
Show.setText(hours + ":" + minutes + ":" + seconds);
}
});
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_update);
Show = (TextView) findViewById (R.id.time);
Timer t = new Timer();
t.scheduleAtFixedRate(timer , 0 , 5000);
}
}
将实际计时器(java.util.Timer)与runOnUiThread()结合使用是解决此问题的一种方法,下面是如何实现它的示例
public class myActivity extends Activity {
private Timer myTimer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
// Set your textView data here.
//Do something to the UI thread here
}
};
}
使用Play Service提供的PeriodicTask,这是谷歌最新的安排工作背景的工具。你想完成什么样的任务。您可以使用计时器或处理程序。顺便说一句,你的第二个活动中有两个计时器对象。如果你添加了一个指向文档的链接,那么读者就可以省去一些谷歌搜索了:)