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. jQuery还原select下拉列表和清空input的值,回显下拉列表框的值

    实现用jQuery还原select下拉列表的值,用了很多种方式,花了一些时间,最后重要找到一种可以实现的方式, 页面上有这些内容 <select id ="level" na ...

  2. Excel报表开发

    读取Excel数据 /// <summary> /// 封装方法 /// </summary> /// <param name="path">& ...

  3. 关于UGUI Image Sliced模式的一个BUG。

    Unity4.6.2f1 在Android/IOS平台下,Image选择Sliced模式,并且对Sprite设置好Border后,会发现并没有按照预计的 情况进行拉伸. 搜了一下是因为Sprite的G ...

  4. 在PHP与HTML混合输入的页面或者模板中就需要对PHP代码进行闭合

    PHP程序的时候会在文件的最后加上一个闭合标签,如下: <?phpclass MyClass{public function test(){//do something, etc.}}?> ...

  5. fio

    h3.western { font-family: "Liberation Sans", sans-serif; font-size: 14pt } h3.cjk { font-f ...

  6. GFS文件系统和在RedHat Linux下的配置

    GFS的全称是Google file System,为了满足Google迅速增长的数据处理要求,Google设计并实现的Google文件系统(GFS).Google文件系统是一个可扩展的分布式文件系统 ...

  7. 【SIGGRAPH】用【有说服力的照片真实】技术实现最终幻想15的视觉特效

    原文:西川善司 http://www.4gamer.net/games/075/G007535/20160726064/   最终幻想15的演讲会场.相当大,听众非常多.      在本次计算机图形和 ...

  8. 奥迪--Q5

    -型号:Q5 -价格:40-53W -动力:2T -变速箱:8挡手自一体 -长宽高:4.63,1.90,1.66 -油箱:75L -发动机:EA888 -大灯:氙气

  9. NEC学习 ---- 模块 -多行式面包屑导航

    如上面形式面包屑的写法: HTML如下, <div class="m-crumb"> <ul class="f-cb"> <li& ...

  10. iOS 数组内中英文混合排序

    NSInteger sortObjects(id obj1, id obj2,void *context) { NSMutableString * str1 = [[NSMutableString a ...