Android学习笔记——Bundle
2015-06-15 15:33
197 查看
该工程的功能是实现不同线程之间数据的传递
以下代码是MainActivity.java中的代码
以下代码是activity_main.xml中的代码
以下代码是MainActivity.java中的代码
package com.example.bundle; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import android.os.Message; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //打印了当前线程的ID System.out.println("Activity-->" + Thread.currentThread().getId()); //生成一个HandleThread对象,实现了使用Looper来处理消息队列的功能,这个类由android程序框架提供 HandlerThread handlerThread = new HandlerThread("handler_thread"); //在使用HandlerThread的getLooper()方法之前,必须先调用该类的start() handlerThread.start(); MyHandler myHandler = new MyHandler(handlerThread.getLooper()); Message msg = myHandler.obtainMessage(); //msg.obj="abc"; //将msg发送到目标对象,所谓的目标对象,就是生成该msg对象的handler对象 Bundle b = new Bundle(); b.putInt("age", 20); b.putString("name", "John"); msg.setData(b); msg.sendToTarget(); } class MyHandler extends Handler{ public MyHandler(){ } public MyHandler(Looper looper){ //super调用父类 super(looper); } public void handleMessage(Message msg){ //String s = (String)msg.obj; Bundle b = msg.getData(); int age = b.getInt("age"); String name = b.getString("name"); System.out.println("age is " + age +", name is " + name); System.out.println("Handler-->" + Thread.currentThread().getId()); System.out.println("handlerMessage"); } } }
以下代码是activity_main.xml中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LinearLayout>
相关文章推荐
- Android ViewPager 引导页的一种效果
- Android自定义View—NumberPicker的使用
- Android自定义AlertDialog去掉黑边的方法
- Intel 英特尔之开发人员专区 ------Android 开发之Android 应用程序如何调用支付宝接口
- 关于在TextView中设置DrawableLeft的问题
- Android自定义View—HorizontalSlideDeleteListView
- Android自定义View—SildeMenu
- android 开发环境的搭建
- Android整机源码如何轻松下载?清华大学Android源码镜像
- Android自定义View—侧边栏菜单
- Android自定义View—自定义的DatePicker
- androidの监听EditView中的文本TextWatcher
- Android ViewDragHelper 实现 QQ5.0 侧滑
- Openfire + Smack 4.1.1 搭建Android聊天平台
- Android实用方法—返回键的监听
- Android优化系列——性能优化(资源存储)
- Android自定义View的实现方法,带你一步步深入了解View(四)
- Android报错"case expressions must be constant expressions"
- Design Support Library —— TextInputLayout
- android之ScrollView里嵌套ListView(转)