什么是AysncTask??
让我想想,能够在异步任务进行的同时,将任务进度状态反馈给 UI 线程(如让 UI 线程更新进度条)。
AsyncTask直接继承object类,位于android.os包中,可以便捷的完成异步耗时操作。
方法举例?
Zzzzzzzzzzzzzzzzzzzzzzzz.........................Zzzzzzzzzz4个方法:onPreExecute()、doInBackground(String... params)、onProgressUpdate()、onPostExecute()四个方法。你今天要是继续打瞌睡,就站在外面
好的(胆怯)
具体实现方法呢
public
class
MainActivity
extends
AppCompatActivity {
private
int
minute =
30
;
//这是分钟
private
int
second =
0
;
//这是分钟后面的秒数。这里是以30分钟为例的,所以,minute是30,second是0
private
TextView timeView;
private
Timer timer;
private
TimerTask timerTask;
//这是接收回来处理的消息
private
Handler handler =
new
Handler() {
public
void
handleMessage(Message msg) {
if
(minute ==
0
) {
if
(second ==
0
) {
timeView.setText(
"Time out !"
);
if
(timer !=
null
) {
timer.cancel();
timer =
null
;
}
if
(timerTask !=
null
) {
timerTask =
null
;
}
}
else
{
second--;
if
(second >=
10
) {
timeView.setText(
"0"
+ minute +
":"
+ second);
}
else
{
timeView.setText(
"0"
+ minute +
":0"
+ second);
}
}
}
else
{
if
(second ==
0
) {
second =
59
;
minute--;
if
(minute >=
10
) {
timeView.setText(minute +
":"
+ second);
}
else
{
timeView.setText(
"0"
+ minute +
":"
+ second);
}
}
else
{
second--;
if
(second >=
10
) {
if
(minute >=
10
) {
timeView.setText(minute +
":"
+ second);
}
else
{
timeView.setText(
"0"
+ minute +
":"
+ second);
}
}
else
{
if
(minute >=
10
) {
timeView.setText(minute +
":0"
+ second);
}
else
{
timeView.setText(
"0"
+ minute +
":0"
+ second);
}
}
}
}
}
};
@Override
protected
void
onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timeView = (TextView) findViewById(R.id.tv);
timeView.setText(minute +
":"
+ second);
timerTask =
new
TimerTask() {
@Override
public
void
run() {
Message msg =
new
Message();
msg.what =
0
;
handler.sendMessage(msg);
}
};
timer =
new
Timer();
timer.schedule(timerTask,
0
,
1000
);
}
@Override
protected
void
onDestroy() {
if
(timer !=
null
) {
timer.cancel();
timer =
null
;
}
if
(timerTask !=
null
) {
timerTask =
null
;
}
minute = -
1
;
second = -
1
;
super
.onDestroy();
}
@Override
protected
void
onStart() {
super
.onStart();
}
@Override
protected
void
onStop() {
super
.onStop();
}
@Override
protected
void
onResume() {
super
.onResume();
}
@Override
protected
void
onRestart() {
super
.onRestart();
}
@Override
protected
void
onPause() {
super
.onPause();
}
}
(倒计时30min操作)
好了,还有什么问题我忘了(其实是垃圾博主忘了题目),下课!