handler.post()方法的执行时机

有如下场景,在onCreate()方法里执行handler.post(new Runnable())

public class ProgressBarActivity extends Activity {
private final static String TAG = "ProgressBarActivity";

private Runnable test = new Runnable(){
    @Override
    public void run() {
        try {
            Thread.sleep(10000);
            Log.i(TAG,"Thread---->"+Thread.currentThread().getId());
            Log.i(TAG,"Thread---->"+Thread.currentThread().getName());
        } catch (InterruptedException e) {}
    }
};
private Handler handler = new Handler(){
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    handler.post(test);
    this.setContentView(R.layout.progress_bar_layout);
    Log.i(TAG,"Activity--->"+Thread.currentThread().getId());
    Log.i(TAG,"Activity--->"+Thread.currentThread().getName());
} 

以上代码里的test会立马执行或者说在activity的log日志前执行吗?
看下打印结果:

06-04 14:13:09.964: INFO/ProgressBarActivity(366): Activity--->1

06-04 14:13:09.964: INFO/ProgressBarActivity(366): Activity--->main

06-04 14:13:20.070: INFO/ProgressBarActivity(366): Thread---->1

06-04 14:13:20.070: INFO/ProgressBarActivity(366): Thread---->main

都知道post(new Runnable())也是在主线程里执行的,那为什么从打印结果上看是先走完oncreate方法再执行runnable里的方法呢?这个要从handler原理说起:

  • 当Android应用程序启动时,framework会为该应用程序的主线程创建一个Looper对象。这个Looper对象包含一个简单的消息队列Message Queue,并且能够循环的处理队列中的消息。这些消息包括大多数应用程序framework事件,例如Activity生命周期方法调用、button点击等,这些消息都会被添加到消息队列中并被逐个处理。

所以Activity生命周期方法调用其实也是通过handler来处理的,oncreate调用和post(new Runnable())对于handler来说都是一个message的处理,都存在主线程的message queue里,但队列是FIFO原则,所以Runnable()必须等oncreate这条message处理完才能处理自己的这条message。

You have it wrong: the docs here say this:

Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached. 
That means the Runnable will be run on the Activity's main thread, but will be queued up and run AFTER the thread is done with its current work. The current work is completing onCreate, so after onCreate finishes the thread is now free and will process the Runnable. This is why you see the Activity text before the Runnable text: the current thread can not just stop what its doing and pick up the Runnable.

关于以上demo相关的stackoverflow地址

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android中,一个项目的Application类是一个全局单例,用于初始化应用程序的全局状态。下面是一个简单的Application类的实现: ```java public class MyApp extends Application { private static MyApp instance; public static MyApp getInstance() { return instance; } @Override public void onCreate() { super.onCreate(); instance = this; // 初始化全局状态 // ... } } ``` 上述代码中,我们定义了一个名为MyApp的Application类,并在其中实现了一个getInstance()方法,用于获取全局单例。在onCreate()方法中,我们可以进行一些全局状态的初始化操作。 如果我们需要在Application类中进行一些复杂的初始化操作,可以考虑使用一个单独的线程来完成。例如,下面是一个在Application类中进行网络请求并缓存结果的实现: ```java public class MyApp extends Application { private static MyApp instance; public static MyApp getInstance() { return instance; } @Override public void onCreate() { super.onCreate(); instance = this; // 初始化全局状态 // ... // 在单独的线程中进行网络请求 new Thread(new Runnable() { @Override public void run() { // 进行网络请求 // ... // 缓存结果 // ... } }).start(); } } ``` 当然,如果我们需要在Application类中进行复杂的初始化操作,还可以使用一些开源库,例如Dagger2、ButterKnife等。这些库可以帮助我们更方便地进行依赖注入、View绑定等操作,从而简化Application类的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值