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 多线程练习的更多相关文章

  1. Android网络多线程断点续传下载

    本示例介绍在Android平台下通过HTTP协议实现断点续传下载. 我们编写的是Andorid的HTTP协议多线程断点下载应用程序.直接使用单线程下载HTTP文件对我们来说是一件非常简单的事.那么,多 ...

  2. 教你如何在 Android 使用多线程下载文件

    # 教你如何在 Android 使用多线程下载文件 前言 在 Android 日常开发中,我们会经常遇到下载文件需求,这里我们也可以用系统自带的 api DownloadManager 来解决这个问题 ...

  3. Android进阶——多线程系列之Thread、Runnable、Callable、Future、FutureTask

    多线程一直是初学者最抵触的东西,如果你想进阶的话,那必须闯过这道难关,特别是多线程中Thread.Runnable.Callable.Future.FutureTask这几个类往往是初学者容易搞混的. ...

  4. android handle详解3 ThreadHandler

    在android handle详解2的基础上,我们来学习ThreadHandler ThreadHandler的本质就是对android handle详解2的实现 HandlerThread其实还是一 ...

  5. 36.Android之多线程和handle更新UI学习

    android经常用到多线程更新UI,今天学习下. 首先布局比较简单: <?xml version="1.0" encoding="utf-8"?> ...

  6. Android之——多线程下载演示样例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46883927 一.概述 说到Android中的文件下载.Android API中明 ...

  7. android开发--多线程

    android中的几种多线程实现方式: 1)Activity.runOnUiThread(Runnable) 2)View.post(Runnable) ;View.postDelay(Runnabl ...

  8. (原创)android Sqlite多线程访问异常解决方案

    在开发Android的程序的时候sqlite数据库是经常用到的:在多线程访问数据库的时候会出现这样的异常:java.lang.IllegalStateException: Cannot perform ...

  9. Android之多线程断点下载

    本文主要包含多线程下载的一些简单demo,包括三部分 java实现 android实现 XUtils开源库实现 注意下载添加网络权限与SD卡读写权限 java实现多线程下载 public class ...

随机推荐

  1. spring框架和junit框架结合使用案例

    package ltssh; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.be ...

  2. CCLabelAtlas

    1.CCLabelAtlas 的具体用法 CCLabelAtlas *pLabel = new CCLabelAtlas;pLabel ->initWithString("0123&q ...

  3. IO 磁盘总结

    IO 磁盘 1.读取 首先引用一个using System.IO;引用空间其次 string ste= File.ReadAllText("E:\\bt.txt",Encoding ...

  4. windows 快捷键

    Windows 系统 f6  在同一个应用的不同窗口进行切换 ctrl-shift 拖动,创建文件快捷方式 shift 右键点击文件 可以出现复制路径的菜单 WIN键组合键 Windows Key + ...

  5. A trip through the Graphics Pipeline 2011_06_(Triangle) rasterization and setup

    Welcome back. This time we’re actually gonna see triangles being rasterized – finally! But before we ...

  6. 序列化(Serialization)据为JSONP远端请求

    Insus.NET前些日子,有分享了一段代码,<使用JSONP跨域请求数据>http://www.cnblogs.com/insus/p/3512271.html 是使用jQuery的Da ...

  7. 【五子棋AI循序渐进】——开局库

    首先,对前面几篇当中未修复的BUG致歉,在使用代码时请万分小心…………尤其是前面关于VCF\VCT的一些代码和思考,有一些错误.虽然现在基本都修正了,但是我的程序还没有经过非常大量的对局,在这之前,不 ...

  8. cocos2d-x-3.11.1 初使用

    1. 引擎子系统包括: 世界编辑器.渲染系统.人机交互系统.动画系统.音频系统.物理引擎.网络接口 等 2. cocos2d-x 特点:开源的.跨平台的. cocos2d-x的发展过程: cocos2 ...

  9. 整理常用的iOS第三方资源

    一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://github. ...

  10. ubuntu dpkg 依赖问题处理

    ubuntu dpkg 依赖问题处理 使用 apt-get 安装软件期间,如果出现意外中断的情况,下次安装时会出现 dpkg 的一系列依赖问题,提示如下 :: dpkg: error processi ...