码迷,mamicode.com
首页 > 移动开发 > 详细

[Android] Android 定时任务实现的三种方法(以SeekBar的进度自动实现为例)

时间:2019-04-05 00:22:11      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:cli   listen   star   max   this   ott   public   test   catch   

 [Android] Android 定时任务实现的三种方法(以SeekBar的进度自动实现为例)

 

一、采用Handler与线程的sleep(long)方法
二、采用Handler与timer及TimerTask结合的方法
三、采用Handler的postDelayed(Runnable, long)方法

 

 布局文件: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".activity.MainActivity">

    <SeekBar
        android:id="@+id/seek_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxHeight="5.0dp"
        android:minHeight="5.0dp"
        android:progressDrawable="@drawable/sb_bar"
        android:thumb="@drawable/sb_thumb"
        />

    <TextView
        android:id="@+id/txt_cur"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" />

    <Button
        android:id="@+id/btn_start1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="start1"
        android:text="第一种方法启动"/>
    <Button
        android:id="@+id/btn_start2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="start2"
        android:text="第二种方法启动"/>
    <Button
        android:id="@+id/btn_start3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="start3"
        android:text="第三种方法启动"/>

</LinearLayout>

 

 MainActivity.java 响应代码:

package com.jack.testprogress.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import com.jack.testprogress.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Android 定时任务实现的三种方法 ,推荐方法3
 * 以SeekBar的进度自动实现,为例
 *
 */
public class MainActivity extends AppCompatActivity {
    private final String TAG = "seek_bar";
    private Context mContext;
    private SeekBar seek_bar;
    private TextView txt_cur;
    private int progressStart = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = MainActivity.this;

        txt_cur = (TextView) findViewById(R.id.txt_cur);
        seek_bar = (SeekBar) findViewById(R.id.seek_bar);

        seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                txt_cur.setText("当前进度值:" + progress + "  / 100 ");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }

    //发送方法1, 新建handleMessage接收消息,然后再新建Thread发送(java方法)
    protected void start1(View v) {
        progressStart = 0; //每次操作前 先重置 progressStart

        //接收消息,并更新
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 123) {
                    seek_bar.setProgress(progressStart);
                }
            }
        };
        new Thread() {
            @Override
            public void run() {
                while (progressStart < 100) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    progressStart++;
                    Log.i(TAG, "int:" + progressStart);
                    handler.sendEmptyMessage(123);
                }
            }
        }.start();
    }

    //发送方法2, 新建handleMessage接收消息,Timer+TimerTask定时器发送
    protected void start2(View v) {
        progressStart = 0; //每次操作前 先重置 progressStart

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 123) {
                    seek_bar.setProgress(progressStart);
                }
            }
        };
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                if (progressStart < 100) { //注意此处是if,而不是死循环while
                    progressStart++;
                    Log.i(TAG, "int:" + progressStart);
                    handler.sendEmptyMessage(123);
                }
            }
        };
        timer.schedule(timerTask, 100, 100);

    }

    //方法3,Handler + Runnable,推荐多使用此方法
    protected void start3(View v) {
        progressStart = 0; //每次操作前 先重置 progressStart

        final int interval_time = 100;//间隔时间 100毫秒
        final Handler handler = new Handler();
        handler.postDelayed(
                new Runnable() {
                    @Override
                    public void run() {
                        //要做的事情
                        if (progressStart < 100) { //进度小于100
                            progressStart++;
                            Log.i(TAG, "int:" + progressStart);
                            seek_bar.setProgress(progressStart);
                            handler.postDelayed(this, interval_time);
                        } else {
                            Log.i(TAG, "cancel int:" + progressStart);
                            handler.removeCallbacks(this); //完成任务后,取消定时器
                        }
                    }
                }
                , interval_time); //启动发送
    }


}

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/10657659.html

转载请著名出处!谢谢~~

 

[Android] Android 定时任务实现的三种方法(以SeekBar的进度自动实现为例)

标签:cli   listen   star   max   this   ott   public   test   catch   

原文地址:https://www.cnblogs.com/wukong1688/p/10657659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!