您的位置:首页 > 移动开发 > Android开发

Android学习笔记——Handler(二)

2015-06-17 17:04 369 查看
对比请看http://blog.sina.com.cn/s/blog_78c913e30100uqmf.html

以下代码是MainActivity.java中的代码

package com.example.handlertest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends Activity {

private Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
handler.post(r);
//Thread t = new Thread(r);
//t.start();
System.out.println("activity--->"+ Thread.currentThread().getId());
System.out.println("activityname--->"+ Thread.currentThread().getName());
}

Runnable r = new Runnable(){
public void run() {
System.out.println("handler--->"+ Thread.currentThread().getId());
System.out.println("handlername--->"+ Thread.currentThread().getName());
try{
Thread.sleep(10000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
};
}


以下代码是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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: