Android 定时器任务问题
Android Timer Task Issue
我在加载 ListView 时需要一个计时器。像“ Waiting [(Timer) 42] Seconds for buy elements” 我想向用户展示 Textview 喜欢它。我无法在 ListView 中使用 Thread...
我从 runOnUiThread 收到错误。为什么 ?我无法在 Listview 中使用计时器。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final TextView LblTime;
inflater=(LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.test,parent,false);
LblTime=(TextView)view.findViewById(R.id.LblFragmentAnaListViewKalanSure );
Timer T=new Timer();
T.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Context.getApplicationContext().runOnUiThread(new Runnable() {
@Override
public void run() {
long Time= MyTime.get(position).getTime() - (new Date()).getTime();
long Sec= Time/ 1000 % 60;
long Min= Time/ (60 * 1000) % 60;
LblTime.setText(Min+":"+Sec);
}
});
}
}, 1000, 1000);
return view;
}
尝试使用 CountDownTimer 它在 UI 线程中完美运行。您所需要的只是像这样在您的代码中启动它:
timer = new CountDownTimer(MS_TILL_COMPLETE, TICK_OFFSET) {
@Override
public void onTick(long l) {
//Repeating every TICK_OFFSET
}
@Override
public void onFinish() {
//Called after MS_TILL_COMPLETE
}
}.start();
尝试使用 CountDownTimer 而不是计时器
new CountDownTimer(10000, 20000) {
@Override
public void onTick(long millisUntilFinished) {}
public void onFinish() {
}
}.start();
试试这个.. 通话后 42 秒..
(new Handler()).postDelayed(new Runnable() {
public void run() {
// do you want
}
}, 42000);
如果每秒钟你都需要这样做..
This is using the Handler
初始化..
Handler handler = new Handler();
导入
import android.os.Handler;
打电话给..
handler.postDelayed(yourtask,42*1000);
private Runnable yourtask = new Runnable() {
public void run() {
// Run your code..
handler.removeCallbacks(yourtask);
handler.postDelayed(yourtask, 42*1000); // again start...
}
};
This is Using Timer every one second....
final Handler handler = new Handler();
Timer timer = new Timer();
private int DELAY = 1000 * 60 * 1;
称之为
timer.scheduleAtFixedRate(doAsynchronousTask, 0, DELAY);
在class
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
// your code
}
});
}
};
我在加载 ListView 时需要一个计时器。像“ Waiting [(Timer) 42] Seconds for buy elements” 我想向用户展示 Textview 喜欢它。我无法在 ListView 中使用 Thread...
我从 runOnUiThread 收到错误。为什么 ?我无法在 Listview 中使用计时器。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final TextView LblTime;
inflater=(LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.test,parent,false);
LblTime=(TextView)view.findViewById(R.id.LblFragmentAnaListViewKalanSure );
Timer T=new Timer();
T.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Context.getApplicationContext().runOnUiThread(new Runnable() {
@Override
public void run() {
long Time= MyTime.get(position).getTime() - (new Date()).getTime();
long Sec= Time/ 1000 % 60;
long Min= Time/ (60 * 1000) % 60;
LblTime.setText(Min+":"+Sec);
}
});
}
}, 1000, 1000);
return view;
}
尝试使用 CountDownTimer 它在 UI 线程中完美运行。您所需要的只是像这样在您的代码中启动它:
timer = new CountDownTimer(MS_TILL_COMPLETE, TICK_OFFSET) {
@Override
public void onTick(long l) {
//Repeating every TICK_OFFSET
}
@Override
public void onFinish() {
//Called after MS_TILL_COMPLETE
}
}.start();
尝试使用 CountDownTimer 而不是计时器
new CountDownTimer(10000, 20000) {
@Override
public void onTick(long millisUntilFinished) {}
public void onFinish() {
}
}.start();
试试这个.. 通话后 42 秒..
(new Handler()).postDelayed(new Runnable() {
public void run() {
// do you want
}
}, 42000);
如果每秒钟你都需要这样做..
This is using the Handler
初始化..
Handler handler = new Handler();
导入
import android.os.Handler;
打电话给..
handler.postDelayed(yourtask,42*1000);
private Runnable yourtask = new Runnable() {
public void run() {
// Run your code..
handler.removeCallbacks(yourtask);
handler.postDelayed(yourtask, 42*1000); // again start...
}
};
This is Using Timer every one second....
final Handler handler = new Handler();
Timer timer = new Timer();
private int DELAY = 1000 * 60 * 1;
称之为
timer.scheduleAtFixedRate(doAsynchronousTask, 0, DELAY);
在class
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
// your code
}
});
}
};