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. Web服务器与Servlet容器

    今日要闻: Oracle启动了JRE7到JRE8的自动更新, JRE8发布于2014.3,于2014.10成为java.com默认版本, JRE7发布于2011.7, Oracle指定的Java生命政 ...

  2. 基于AWS的自动化部署实践

    过年前,我给InfoQ写了篇文章详细介绍我们团队在过去4年基于AWS的自动化部署实践.文章包括了:为什么选择AWS.AWS上自动化部署的优势和挑战.我们的解决方案,以及和AWS DevOps方案(Op ...

  3. js混淆工具

    1\  http://www.jasob.com 2\ http://developer.yahoo.com/yui/compressor

  4. Hadoop HDFS概念学习系列之分布式文件管理系统(二十五)

    数据量越来越多,在一个操作系统管辖的范围存在不了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来 管理多台机器上的文件,这就是分布式文件管理系统. 是一种允许文件 ...

  5. 详解Android定位

    相信很多的朋友都有在APP中实现定位的需求,今天我就再次超炒冷饭,为大家献上国内开发者常用到的三种定位方式.它们分别为GPS,百度和高德,惯例先简单介绍下定位的背景知识. 什么是GPS定位.基站定位和 ...

  6. angular的directive笔记

    原贴地址 1,tansclude: 是指令能够能够把外部定义的内容传回指令模板内部(通过在内部标签使用ng-transclude).这个外部指定的内容是根据外部的作用域控制的,跟指令的作用域无关.这个 ...

  7. Debugging Information in Separate Files

    [Debugging Information in Separate Files] gdb allows you to put a program's debugging information in ...

  8. HDU 2147 kiki's game (简单博弈,找规律)

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/1000 K (Java/Others)Total ...

  9. SharePoint 2013的100个新功能之社交

    一:社会能力 SharePoint 2013引入了一个新东西叫做社会能力,使公司组织中的用户社会化协作.我的网站难以置信地做了改进以集成社会能力.除了我的网站,新的社区网站(新闻提要),关注用户和关注 ...

  10. RecyclerView 下拉刷新上拉加载

    步骤: 首先直接定义一个XRecyclerView继承RecyclerView,重写他的三个构造方法. init(Context mContext)方法用来初始化底部加载的view 回到XRecycl ...