Android开发之Handler(2)
2016-05-16 19:22
453 查看
WorkThread发送消息,在MainThread中接受消息,并且利用该消息处理UI。
<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/textViewId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/buttonId" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/textViewId" android:text="发送消息" /></RelativeLayout>
MainActivity.javapackage com.example.handlernew1;import android.app.Activity;import android.graphics.MaskFilter;import android.os.Bundle;import android.os.Handler;import android.os.Message;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 TextView textView; private Button button; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textViewId); button = (Button)findViewById(R.id.buttonId); handler = new MyHandler(); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { Thread t=new NetworkThread(); t.start(); } }); } class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { System.out.println("handleMessage-------->"+Thread.currentThread().getName()); String s= (String)msg.obj; textView.setText(s); } } class NetworkThread extends Thread{
@Override public void run() { System.out.println("networkthread-------->"+Thread.currentThread().getName()); // 模拟访问网络,当线程运行时,首先休眠两分钟 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } String s="从网络中获取的数据"; Message msg=new Message(); msg.obj = s; //sendMessage()方法,无论是在主线程还是在Worker Thread当中,都是可以的。 handler.sendMessage(msg); }
}
@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; }
}
<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/textViewId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/buttonId" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/textViewId" android:text="发送消息" /></RelativeLayout>
MainActivity.javapackage com.example.handlernew1;import android.app.Activity;import android.graphics.MaskFilter;import android.os.Bundle;import android.os.Handler;import android.os.Message;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 TextView textView; private Button button; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textViewId); button = (Button)findViewById(R.id.buttonId); handler = new MyHandler(); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { Thread t=new NetworkThread(); t.start(); } }); } class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { System.out.println("handleMessage-------->"+Thread.currentThread().getName()); String s= (String)msg.obj; textView.setText(s); } } class NetworkThread extends Thread{
@Override public void run() { System.out.println("networkthread-------->"+Thread.currentThread().getName()); // 模拟访问网络,当线程运行时,首先休眠两分钟 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } String s="从网络中获取的数据"; Message msg=new Message(); msg.obj = s; //sendMessage()方法,无论是在主线程还是在Worker Thread当中,都是可以的。 handler.sendMessage(msg); }
}
@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; }
}
相关文章推荐
- Android开发之Handler
- Android 开发中的线程
- Android学习笔记十五:wifi连接,获取名字,IP地址
- TextView整理
- Android学习——Notification
- Android如何将图片上传到七牛云存储
- Android自定义View之仿知乎滑动删除Activity
- android imageButton 点击按钮前中后,按钮颜色的变化
- 国内下载android源码镜像地址及方法
- Android 热补丁动态修复框架小结
- Android学习——Menu
- 初始Volley框架 ImageLoader请求Url 并加载
- android animation——view进入退出动画
- Android学习——Dialog
- AndroidStudio目录结构视图解说,androidstudio视图
- Android屏幕适配方案(个人觉得不错,比percent-support-lib好用)
- setContentView()与LayoutInflater.from()的区别-android
- 史上最全的Android ViewDragHelper解析
- 从头学android_短信发送器
- Android dex分包方案