Android 进度暂停和继续
Android 进度暂停和继续
文章目录
- Android 进度暂停和继续
- 目录
- 前言
- 实现效果
- 正文
- 定义线程方法
- 定义回调方法和消息发送方法
- 界面的实现
- MainActivity()
- 最后
目录
前言
上一篇文章实现了Java线程的暂停和继续,那么我们接下来就应该在Android 当中通过界面的显示来清楚明白的让大家看到暂停和继续。
实现效果
正文
定义线程方法
public class MyThread extends Thread{private final Object object = new Object();private boolean pause = false;private int Max = 0;/**设置暂停 为 ture*/public void pauseThread() {pause = true;}/** 设置暂停为false,并继续*/public void rsumeThread() {pause = false;synchronized (object) {object.notify();}}/**暂停方法*/public void onPause() {synchronized (object) {try {object.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}@Overridepublic void run() {super.run();int index = 1;while (true) {while (pause) {onPause();}try {if (index <= 100) {MsgHandleUtil.getInstance().sendNormalMsg(1, null, index);//通过回调发送消息到UI界面并更新UISystem.out.println("index = " + index);sleep(1000);index++;}} catch (InterruptedException e) {e.printStackTrace();break;}}}}
和上一篇文章基本一样,但是在这个类中我们加入了相应的方法来进行数据的传递,我们创建了相应的类MsgHandleUtil
类,该类对数据进行了分类和使用
定义回调方法和消息发送方法
首先看一定义的回调接口类I_MsgCallBack
public interface I_MsgCallBack{void msgCallBack(int what,Object obj,int ... arg);}
以上就是一个接口类的方法,我们可以在相应的方法中进行实现就OK了,那么这边可以先放着,稍后会在实现的时候去说明;
接下来就是消息发送方法类MsgHandleUtil
public class MsgHandleUtil{private I_MsgCallBack callBack = null;private static MsgHandleUtil errUtil = new MsgHandleUtil();private MsgHandleUtil(){}public static MsgHandleUtil getInstance(){return errUtil;}public void init(I_MsgCallBack callBack){this.callBack = callBack;}public void sendNormalMsg(int what, Object obj, int... arg){if (arg.length == 1){callBack.msgCallBack(what, obj, arg[0]);} else if (arg.length == 2){callBack.msgCallBack(what, obj, arg[0], arg[1]);} else{callBack.msgCallBack(what, obj);}}}
以上只是简单的使用了单例模式
,简单的说明一下,单例模式就是可以通过方法获取到类的对象,真正的单例模式需要考虑很多东西,如:这个类只能有一个对象,所有调用的都是这一个对象。当然我这个不需要,所以就没有这样写, init()
方法,是需要在相应的方法初始化一下callback
方法,初始化后就可以正常使用回调了,最后一个方法就是通过回调进行数据的传输sendNormalMsg()
,其中的参数相当于Handler
中msg
的方法一一对应的.
界面的实现
<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" 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="com.example.testthread.MainActivity" > <ProgressBar android:id="@+id/progressbar" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="0" /> <Button android:id="@+id/pause" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/progressbar" android:text="暂停" /> <Button android:id="@+id/continued" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/pause" android:text="继续" />RelativeLayout>
其中就是一个progressbar
和两个button
;
MainActivity()
ublic class MainActivity extends ActionBarActivity implements OnClickListener,I_MsgCallBack{private Button continued, pause;private ProgressBar bar;private static MainActivity activity = null;private MyThread myThread = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);MsgHandleUtil.getInstance().init(this);//初始化回调方法continued = (Button) findViewById(R.id.continued);pause= (Button) findViewById(R.id.pause);bar = (ProgressBar) findViewById(R.id.progressbar);updateUI();continued.setOnClickListener(this);pause.setOnClickListener(this);} /* *更新UI,创建线程,并对线程进行操作 */private void updateUI() {if (myThread == null) {myThread = new MyThread();}Thread thread = new Thread(myThread);thread.start();}@Overridepublic void onClick(View arg0) {switch (arg0.getId()) {case R.id.continued: //继续myThread.rsumeThread();break;case R.id.pause: //暂停myThread.pauseThread();break;default:break;}} /* *通过回调方法对数据进行发送 */@Overridepublic void msgCallBack(int what ,Object obj, int... arg) {Message msg = new Message();msg.obj = obj;msg.what = what;if (arg.length == 1) {msg.arg1 = arg[0];} else if (arg.length == 2) {msg.arg1 = arg[0];msg.arg2 = arg[1];}handler.sendMessage(msg);} /* * Handler 方法,接收回调的方法 */private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case 1: //对UI进行更新bar.setProgress(msg.arg1);break;default:break;}}};}
以上就是主方法了,那么首先就是实现了回调接口和OnClick
接口,并在onCreat
方法中对发送信息的回调方法进行了初始化;msgCallBack
就是具体实现了回调的方法,大家可以详细的看一下与Handler
中的msg
相对应;
Handler
中对就收到的数据分析,设置更新UI;
最后
注意:
-
耗时操作必须在子线程中完成
-
不能在子线程中声明主线程的属性
-
更新UI的操作需要通过
Handler
来进行;
希望大家可以自己动手做一遍,我在做的时候发现了很多线程中的困难和报错,查询了很多资料才发现不能这样做,需要那样做,所以还是动手操作一遍比较好!!谢谢大家
更多相关文章
- 【Android 界面效果29】研究一下Android滑屏的功能的原理,及scrol
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- [置顶] Android学习记录(6)—将java中的多线程下载移植到Android中
- Android学习记录(6)—将java中的多线程下载移植到Android中(即多线
- Android 照相机打开方法
- AndEngine添加多个动画精灵的方法
- Android arm模拟器的速度提升方法
- android保存第一次安装状态并初始化数据