HandlerTest2.java
package com.jing.example.nknu_.handlertest2;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
public class HandlerTest2 extends AppCompatActivity {
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handler_test2);
handler.post(r);
System.out.println("activity--->"+ Thread.currentThread().getId());
System.out.println("activityname--->" + Thread.currentThread().getName());
}
Runnable r = new Runnable(){
@Override
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();
}
}
};
}
content_handler_test2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.jing.example.nknu_.handlertest2.HandlerTest2"
tools:showIn="@layout/activity_handler_test2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>