文件名称:Android:Handler的post()方法和Thread的start()方法执行Thread的run()方法的区别
文件大小:1.18MB
文件格式:ZIP
更新时间:2016-11-01 04:25:00
Handler post() Thread start() Android
package com.example.study_5_handlerthread; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* * 创建一个Handler对象,使用Handler对象h把Runnable的对象r压入队列 * 此时只会会执行Run()方法,但是不会开启新的线程 */ //Handler h = new Handler(); //h.post(r); /* * 在java中真正开启线程的方法是这样的。 */ Thread t = new Thread(r); t.start(); setContentView(R.layout.activity_main); System.out.println("Activity-->"+ Thread.currentThread().getId()); System.out.println("Activity-->"+ Thread.currentThread().getName()); } Runnable r = new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Handler-->"+Thread.currentThread().getId()); System.out.println("Handler-->"+Thread.currentThread().getName()); } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
【文件预览】:
Study_5_HandlerThread
----bin()
--------classes.dex(543KB)
--------res()
--------Study_5_HandlerThread.apk(229KB)
--------dexedLibs()
--------classes()
--------resources.ap_(40KB)
--------AndroidManifest.xml(908B)
----res()
--------drawable-ldpi()
--------values-v11()
--------menu()
--------values-v14()
--------drawable-hdpi()
--------drawable-xhdpi()
--------drawable-xxhdpi()
--------values()
--------values-sw720dp-land()
--------drawable-mdpi()
--------values-sw600dp()
--------layout()
----proguard-project.txt(781B)
----ic_launcher-web.png(50KB)
----assets()
----gen()
--------com()
----.settings()
--------org.eclipse.jdt.core.prefs(177B)
----src()
--------com()
----.project(857B)
----.classpath(475B)
----project.properties(563B)
----AndroidManifest.xml(908B)
----libs()
--------android-support-v4.jar(473KB)