Android handle 多线程练习
Android handle
<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=".MainActivity" > <TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_below="@id/text_id" /> </RelativeLayout>
package com.ibox365.s0725001; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private Button button;
private Handler handler; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button=(Button) findViewById(R.id.btn);
button.setOnClickListener(new ButtonListen()); handler=new myHandle(); } class ButtonListen implements OnClickListener
{ /* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub Message msg=handler.obtainMessage();
msg.what=2;
handler.sendMessage(msg);
} } class myHandle extends Handler{ /* (non-Javadoc)
* @see android.os.Handler#handleMessage(android.os.Message)
*/
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg); int what =msg.what;
System.out.println(what); } } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
多线程 操作
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送消息" /> </LinearLayout>
package com.ibox365.s0725002; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity { private Button button;
private TextView textView;
private Handler handler; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button=(Button) findViewById(R.id.btn_id);
button.setOnClickListener(new ButtonListen());
textView=(TextView) findViewById(R.id.text_id);
handler=new MyHandle(); } class ButtonListen implements OnClickListener
{
@Override
public void onClick(View v) {
Thread thread=new MyThread();
thread.start();
}
}
class MyThread extends Thread{ @Override
public void run() {
System.out.println("MyThread==>"+Thread.currentThread().getName()); try {
Thread.sleep(2*1000);
} catch (InterruptedException e) { e.printStackTrace();
} String s="get data from network!";
Message msg=handler.obtainMessage();
msg.obj=s;
handler.sendMessage(msg);
}
} class MyHandle extends Handler{
@Override
public void handleMessage(Message msg) { System.out.println("MyHandle==>"+Thread.currentThread().getName());
String s=(String) msg.obj;
textView.setText(s);
}
} }
思路图:

Android handle 多线程练习的更多相关文章
- Android网络多线程断点续传下载
本示例介绍在Android平台下通过HTTP协议实现断点续传下载. 我们编写的是Andorid的HTTP协议多线程断点下载应用程序.直接使用单线程下载HTTP文件对我们来说是一件非常简单的事.那么,多 ...
- 教你如何在 Android 使用多线程下载文件
# 教你如何在 Android 使用多线程下载文件 前言 在 Android 日常开发中,我们会经常遇到下载文件需求,这里我们也可以用系统自带的 api DownloadManager 来解决这个问题 ...
- Android进阶——多线程系列之Thread、Runnable、Callable、Future、FutureTask
多线程一直是初学者最抵触的东西,如果你想进阶的话,那必须闯过这道难关,特别是多线程中Thread.Runnable.Callable.Future.FutureTask这几个类往往是初学者容易搞混的. ...
- android handle详解3 ThreadHandler
在android handle详解2的基础上,我们来学习ThreadHandler ThreadHandler的本质就是对android handle详解2的实现 HandlerThread其实还是一 ...
- 36.Android之多线程和handle更新UI学习
android经常用到多线程更新UI,今天学习下. 首先布局比较简单: <?xml version="1.0" encoding="utf-8"?> ...
- Android之——多线程下载演示样例
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46883927 一.概述 说到Android中的文件下载.Android API中明 ...
- android开发--多线程
android中的几种多线程实现方式: 1)Activity.runOnUiThread(Runnable) 2)View.post(Runnable) ;View.postDelay(Runnabl ...
- (原创)android Sqlite多线程访问异常解决方案
在开发Android的程序的时候sqlite数据库是经常用到的:在多线程访问数据库的时候会出现这样的异常:java.lang.IllegalStateException: Cannot perform ...
- Android之多线程断点下载
本文主要包含多线程下载的一些简单demo,包括三部分 java实现 android实现 XUtils开源库实现 注意下载添加网络权限与SD卡读写权限 java实现多线程下载 public class ...
随机推荐
- CentOS下IP的配置
1.打开命令窗口,切换成root账户:su - root 2.进入目录:/etc/sysconfig/network-scripts,打开文件vi ifcfg-eth0 3.修改参数: ## 名称DE ...
- apache本地域名ip重定向vhosts
apache本地域名ip重定向,使本机通过指定域名访问到指定ip路径. 1.apache配置apache/conf/httpd.conf : 开启配置 Include conf/extra/http ...
- BizTalk开发系列(四) 深入Map测试
在BizTalk的开发过程中XML消息间的映射是一个很重要的内容.如果只是一般的从源节点的值复制到目标节点的话,BizTalk项目提供的 MAP测试和验证就已经可以满足需求了.但是很多时候需要在映射的 ...
- 点击某个按钮弹出 photoswip
var openPhotoSwipe = function() { var pswpElement = document.querySelectorAll('.pswp')[0]; // build ...
- python 安装mysql-python模块
方式一 使用yum安装 # yum install MySQL-python 方式二 使用pip 安装 # pip install mysql-python 使用pip方式安装需要提前安装如下依赖 m ...
- centos安装配置amoeba以及测试
一.amoeba介绍网址:http://docs.hexnova.com/amoeba/ 二.安装java se1.5 三.安装amoeba2.2.01.下载地址:http://sourceforge ...
- find命令查找文件,并排除相应路径
find / -path "/exclude/" -prune -o -name "lsof" -print 查找根目录下文件,并排除/exclude路径
- Android 更新sdk
http://blog.csdn.net/xiao_ping_ping/article/details/45621663 不FQ意味着不能直接到android官网下载android SDK,但是国内有 ...
- [daily][troubleshoot][archlinux][wps][font] wps文档中的图内容无法显示中文
序 用linux作为工作生产环境的几个需要解决的问题之一是:文档协作,即如何兼容Micro$oft Office格式的文档. 我一般的工作方式是:在linux下创建一个win7的虚拟机,安装常用的wi ...
- spring记录
context:property-placeholder 是将properties加载到Spring上下文中,接下来在定义bean的时候就能用${xx.xx}来访问了. util:properties ...