Android handle 多线程练习
2016-07-25 12:30
344 查看
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 拷贝assets目录下所有文件及文件夹到指定路径
- 【Android】GridView 保持items图片大小一致
- Android内置(外接s)sd卡存储
- Android的数据储存和IO(关于Cursor类后续补充)
- 【Android基础知识】Drawable Animation和View Animation
- Android进程的五个等级
- Android产品研发(十九)-->Android studio中的单元测试
- Android Studio中获取sha1证书指纹数据的方法
- Android so库正确放置姿势
- Android开发实践:掌握Camera的预览方向和拍照方向
- 通过程序打开Android常用系统设置界面
- jinkens_用gradle_构建_android
- Android 使用Vitamio打造自己的万能播放器(4)――本地播放(快捷搜索、数据存储)
- Ubuntu 添加Android SDK及NDK路径到PATH
- android Camera 如何判断当前使用的摄像头是前置还是后置
- 下拉刷新+加载更多的ListView(直接拿来用)
- android-----我眼中的Volley
- Android PreferenceActivity源码分析与使用总结
- 提交androidstudio工程到GitHub
- 产品使用Dagger 2——减少方法数