Android学习笔记——Handler(二)

对比请看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>

 

你可能感兴趣的:(Android学习)