android 主线程与分线程 做同步
直接上代码
package com.example.handlerthreaddemo; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { Test t; Handler mh=new Handler(){ @Override public void handleMessage(Message msg) { String s = (String) msg.obj; Bundle data = msg.getData(); String title = data.getString("title"); String info = data.getString("info"); System.out.println("title is " + title + ", info is" + info); System.out.println("msg.obj is " + s); System.out.println("handlerMessage() is "+Thread.currentThread().getId()); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); t=new Test(mh); } public void OnClick(View v){ switch (v.getId()) { case R.id.button1: t.send(); break; default: break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
分线程代码:
package com.example.handlerthreaddemo; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.Message; public class Test { HandlerThread mhandlerThread1, mhandlerThread2; Handler mhandler1; Handler mhandler2; public Test(Handler mh) { mhandler2=mh; mhandlerThread1 = new HandlerThread("update"); mhandlerThread1.setDaemon(true); mhandlerThread1.start(); mhandler1 = new Handler(mhandlerThread1.getLooper()) { @Override public void handleMessage(Message msg) { String s = (String) msg.obj; Bundle data = msg.getData(); String title = data.getString("title"); String info = data.getString("info"); System.out.println("title is " + title + ", info is" + info); System.out.println("msg.obj is " + s); System.out.println("handlerMessage() is "+Thread.currentThread().getId()); //---------------------------- Message msg1 = new Message(); msg1.obj = "abc"; Bundle data1 = new Bundle(); data1.putString("title", "你好吗"); data1.putString("info", "哈哈哈哈"); msg1.setData(data1); mhandler2.sendMessage(msg1); } }; } public void send() { System.out.println("send() is "+Thread.currentThread().getId()); Message msg = new Message(); msg.obj = "abc"; Bundle data = new Bundle(); data.putString("title", "你好吗"); data.putString("info", "哈哈哈哈"); msg.setData(data); mhandler1.sendMessage(msg); // 将msg发送到对象,所谓的目标对象就是生成该msg对象的Handler对象 } }
xml文件
<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" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignRight="@+id/textView1" android:layout_marginRight="16dp" android:layout_marginTop="44dp" android:text="Button" android:onClick="OnClick" /> </RelativeLayout>
csdn下载路径:http://download.csdn.net/detail/wenwei19861106/4849987
分类:
android_handler
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构