Android消息机制——时钟显示和异步处理工具类(AsyncTask)
1. 时钟显示
定义布局文件——activity_my_analog_clock_thread_demo.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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_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.contactsdemo.MyAnalogClockThreadDemo" > <AnalogClock
android:id="@+id/myAnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
定义Activity程序,进行操作
package com.example.contactsdemo; import java.text.SimpleDateFormat;
import java.util.Date; import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView; public class MyAnalogClockThreadDemo extends ActionBarActivity {
private TextView info = null; //文本显示组件
private static final int SET = 1; //线程标记
private Handler handler = new Handler(){ @Override
public void handleMessage(Message msg) {
switch(msg.what){
case SET: //判断标志位
MyAnalogClockThreadDemo.this.info.
setText("当前时间为:"+msg.obj.toString()); //设置显示信息
}
} };
private class ClockThread implements Runnable{ @Override
public void run() {
while(true){
Message msg = MyAnalogClockThreadDemo.this.handler
.obtainMessage(MyAnalogClockThreadDemo.SET,
new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date())); //实例化Message
MyAnalogClockThreadDemo.this.handler.sendMessage(msg); //发送消息
try {
Thread.sleep(1000); //延迟1秒
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_analog_clock_thread_demo);
this.info = (TextView) super.findViewById(R.id.info); //取得组件
new Thread(new ClockThread()).start(); //启动线程
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_analog_clock_thread_demo, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
2. 异步处理工具类:AsyncTask——实现进度条
定义布局文件——activity_my_async_task_demo.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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_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.contactsdemo.MyAsyncTaskDemo" > <ProgressBar
android:id="@+id/bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
定义Activity程序,显示进度条
package com.example.contactsdemo; import android.support.v7.app.ActionBarActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.TextView; public class MyAsyncTaskDemo extends ActionBarActivity {
private ProgressBar bar = null; //进度条组件
private TextView info = null; //文本显示组件 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_async_task_demo);
this.bar = (ProgressBar) super.findViewById(R.id.bar);
this.info = (TextView) super.findViewById(R.id.info);
ChildUpdate child = new ChildUpdate(); //子任务对象
child.execute(100); //休眠时间
} private class ChildUpdate extends AsyncTask<Integer, Integer, String>{ @Override
protected void onPostExecute(String result) { //任务执行完后执行
MyAsyncTaskDemo.this.info.setText(result); //设置文本
} @Override
protected void onProgressUpdate(Integer... values) { //每次更新后的数值
MyAsyncTaskDemo.this.info.setText("当前进度是:"+values[0]); //更新文本信息
} @Override
protected String doInBackground(Integer... arg0) { //处理后台任务
for(int i=0;i<100;i++){ //进度条累加
MyAsyncTaskDemo.this.bar.setProgress(i); //设置进度
this.publishProgress(i); //传递每次更新的内容
try {
Thread.sleep(arg0[0]); //延缓执行
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "执行完毕!";
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_async_task_demo, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Android消息机制——时钟显示和异步处理工具类(AsyncTask)的更多相关文章
- Android消息机制
每一个Android应用在启动的时候都会创建一个线程,这个线程被称为主线程或者UI线程,Android应用的所有操作默认都会运行在这个线程中. 但是当我们想要进行数据请求,图片下载,或者其他耗时操作时 ...
- Android消息机制不完全解析(上)
Handler和Message是Android开发者常用的两个API,我一直对于它的内部实现比较好奇,所以用空闲的时间,阅读了一下他们的源码. 相关的Java Class: androi ...
- Android 消息机制 (Handler、Message、Looper)
综合:http://blog.csdn.net/dadoneo/article/details/7667726 与 http://android.tgbus.com/Android/androidne ...
- Android开发之漫漫长途 ⅥI——Android消息机制(Looper Handler MessageQueue Message)
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Android开发之漫漫长途 Ⅶ——Android消息机制(Looper Handler MessageQueue Message)
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Android消息机制探索(Handler,Looper,Message,MessageQueue)
概览 Android消息机制是Android操作系统中比较重要的一块.具体使用方法在这里不再阐述,可以参考Android的官方开发文档. 消息机制的主要用途有两方面: 1.线程之间的通信.比如在子线程 ...
- Android消息机制1-Handler(Java层)(转)
转自:http://gityuan.com/2015/12/26/handler-message-framework/ 相关源码 framework/base/core/java/andorid/os ...
- 每日一问:Android 消息机制,我有必要再讲一次!
坚持原创日更,短平快的 Android 进阶系列,敬请直接在微信公众号搜索:nanchen,直接关注并设为星标,精彩不容错过. 我 17 年的 面试系列,曾写过一篇名为:Android 面试(五):探 ...
- 深入理解Android消息机制
在日常的开发中,Android 的消息机制作为系统运行的根本机制之一,显得十分的重要. 从 Handler 发送消息开始 查看源码,Handler的post.send方法最终都会走到 public f ...
随机推荐
- bzoj2940: [Poi2000]条纹
2940: [Poi2000]条纹 条纹游戏是一个双人的游戏.所需要的物品有一个棋盘以及三种颜色的长方形条纹,这三种颜色分别是红色.绿色和蓝色.所有的红色条纹的尺寸是c*1,所有的绿色条纹的尺寸是z* ...
- hdu1753I Hate It(线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=1754 单点更新,区间求最值 // File Name: hdu1754.cpp // Author: bo_jw ...
- IAP内购
IAPHelper.h // // IAPHelper.h // airplay // // Created by apple on 13-10-23. // Copyright (c) 2013年 ...
- 利用 Heritrix 构建特定站点爬虫
http://www.ibm.com/developerworks/cn/opensource/os-cn-heritrix/
- Castle 开发系列文章
转自:http://terrylee.cnblogs.com/archive/2006/04/28/castl_ioc_article.html Castle是针对.NET平台的一个开源项目,从数据访 ...
- contentWindow 和contentDocument区别 及iframe访问
a>contentWindow 兼容各个浏览器,可取得子窗口的 window 对象. b>contentDocument Firefox 支持,> ie8 的ie支持.可取得子窗口的 ...
- (剑指Offer)面试题32:从1到n整数中1出现的次数
题目: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11和12,一共出现了5次. 思路: 1.累加法 累加1到n中每个整数 ...
- OpenCV中图像指针注意点
1.cvQueryFrame方法从摄像头或文件中抓取的帧图像是不能被释放和修改的 2.不要用delete删除,一定要用cvReleaseImage删除且要带有&符号.
- ARM Cortex-M instructions
ARM Cortex-M instruction sets ARMCortex-M Thumb Thumb-2 Hardwaremultiply Hardwaredivide Saturatedmat ...
- Study notes for Clustering and K-means
1. Clustering Analysis Clustering is the process of grouping a set of (unlabeled) data objects into ...