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)的更多相关文章

  1. Android消息机制

    每一个Android应用在启动的时候都会创建一个线程,这个线程被称为主线程或者UI线程,Android应用的所有操作默认都会运行在这个线程中. 但是当我们想要进行数据请求,图片下载,或者其他耗时操作时 ...

  2. Android消息机制不完全解析(上)

        Handler和Message是Android开发者常用的两个API,我一直对于它的内部实现比较好奇,所以用空闲的时间,阅读了一下他们的源码.    相关的Java Class: androi ...

  3. Android 消息机制 (Handler、Message、Looper)

    综合:http://blog.csdn.net/dadoneo/article/details/7667726 与 http://android.tgbus.com/Android/androidne ...

  4. Android开发之漫漫长途 ⅥI——Android消息机制(Looper Handler MessageQueue Message)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  5. Android开发之漫漫长途 Ⅶ——Android消息机制(Looper Handler MessageQueue Message)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  6. Android消息机制探索(Handler,Looper,Message,MessageQueue)

    概览 Android消息机制是Android操作系统中比较重要的一块.具体使用方法在这里不再阐述,可以参考Android的官方开发文档. 消息机制的主要用途有两方面: 1.线程之间的通信.比如在子线程 ...

  7. Android消息机制1-Handler(Java层)(转)

    转自:http://gityuan.com/2015/12/26/handler-message-framework/ 相关源码 framework/base/core/java/andorid/os ...

  8. 每日一问:Android 消息机制,我有必要再讲一次!

    坚持原创日更,短平快的 Android 进阶系列,敬请直接在微信公众号搜索:nanchen,直接关注并设为星标,精彩不容错过. 我 17 年的 面试系列,曾写过一篇名为:Android 面试(五):探 ...

  9. 深入理解Android消息机制

    在日常的开发中,Android 的消息机制作为系统运行的根本机制之一,显得十分的重要. 从 Handler 发送消息开始 查看源码,Handler的post.send方法最终都会走到 public f ...

随机推荐

  1. Java基础 —— JavaScript

    Javascript:基于对象与事件驱动的脚本语言,主要用于客户端 特点: 交互性:信息动态交互. 安全性:不能访问本地硬盘. 跨平台性:只要有浏览器就支持Javascript,与平台无关. Java ...

  2. bzoj3714: [PA2014]Kuglarz

    [PA2014]KuglarzTime Limit: 20 Sec Memory Limit: 128 MBSubmit: 553 Solved: 317[Submit][Status][Discus ...

  3. 第二百二十七天 how can I 坚持

    今天去了蟒山,天池,刚去的时候身体有点难受,整天都是那样,回来就好多了,不知道怎么回事. 天池竟然是个人造池,挺大,没有去十三陵,回来都很晚了. 去天池竟然是走的小路,已经关了,不让进,里边玲玲清清的 ...

  4. ubuntu下设置开机自动挂载硬盘

    我们在linux中常常用mount命令把硬盘分区或者光盘挂载到文件系统中./etc/fstab就是在开机引导的时候自动挂载到linux的文件系统. 如果给计算机配了一块新磁盘,已经分区,格式化,挂载, ...

  5. Microsoft Office Excel 不能访问文件“XXXXXXXXXXXXX.xls”。 可能的原因有:

    解决办法:1. 1).通过webconfig中增加模拟,加入管理员权限, <identity impersonate="true" userName="系统管理员& ...

  6. vim显示历史命令

    [vim显示历史命令] q: 进入命令历史编辑.类似的还有 q/ 可以进入搜索历史编辑.注意 q 后面如果跟随其它字母,是进入命令记录. 可以像编辑缓冲区一样编辑某个命令,然后回车执行.也可以用 ct ...

  7. Labview二进制文件的操作

  8. 找回使用过的QQ头像

    多么渴望那双眼睛能在万紫千红中发现自己:然而眼睛从来就不曾对自己留意:于是换种落寞再次接受垂直打击:然后然后的然后尼玛再换个逗比的头像证明老资无所谓老资无所谓嘿嘿: 我们换头像的时候,发现之前的一个最 ...

  9. GWT工程架构分析与理解

    上一篇文章中介绍了GWT技术的一些理论性的东西,涉及到GWT得一些技术原理及实现.接下来笔者将通过创建一个GWT工程去理解分析GWT工程架构.     GWT工程架构解析 笔者使用的是Eclipse插 ...

  10. Unity3d:编辑器中运行正常,发布后的exe提示找不到文件

    解决方案1:查看文件路径拼写方式,如果是用“+”拼接的,请改用System.IO.Path.Combine()方式拼接.经过测试,两种拼接方式打印出来的路径是一样的,但为什么 加号 的方式拼接unit ...