欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > Android >内容正文

Android

Android消息机制——时钟显示和异步处理工具类(AsyncTask)

发布时间:2025/4/5 Android 52 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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"/><TextViewandroid: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(){@Overridepublic void handleMessage(Message msg) {switch(msg.what){case SET: //判断标志位MyAnalogClockThreadDemo.this.info.setText("当前时间为:"+msg.obj.toString()); //设置显示信息 }}};private class ClockThread implements Runnable{@Overridepublic void run() {while(true){Message msg = MyAnalogClockThreadDemo.this.handler.obtainMessage(MyAnalogClockThreadDemo.SET,new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date())); //实例化MessageMyAnalogClockThreadDemo.this.handler.sendMessage(msg); //发送消息try {Thread.sleep(1000); //延迟1秒} catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();} } }}@Overrideprotected 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(); //启动线程 }@Overridepublic 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;}@Overridepublic 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"/><TextViewandroid: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; //文本显示组件 @Overrideprotected 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>{@Overrideprotected void onPostExecute(String result) { //任务执行完后执行MyAsyncTaskDemo.this.info.setText(result); //设置文本 }@Overrideprotected void onProgressUpdate(Integer... values) { //每次更新后的数值MyAsyncTaskDemo.this.info.setText("当前进度是:"+values[0]); //更新文本信息 }@Overrideprotected 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 "执行完毕!";}}@Overridepublic 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;}@Overridepublic 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)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。