Android,Handler是在主线程还是其他线程中运行? [英] Android, Handler is running in main thread or other thread?

查看:978
本文介绍了Android,Handler是在主线程还是其他线程中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码.

public class SplashScreen extends Activity {
    private int _splashTime = 5000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                 WindowManager.LayoutParams.FLAG_FULLSCREEN);

        new Handler().postDelayed(new Thread(){
           @Override
           public void run(){
             Intent mainMenu = new Intent(SplashScreen.this, MainMenu.class);
             SplashScreen.this.startActivity(mainMenu);
             SplashScreen.this.finish();
             overridePendingTransition(R.drawable.fadein, R.drawable.fadeout);
           }
        }, _splashTime);
    }
}

我在分析此代码时遇到问题.据了解,处理程序正在主线程中运行.但是它有正在其他线程中运行的线程.

MainMenu.class 将在主线程还是第二个线程中运行? 如果主线程停止了5秒钟,则ANR将升高.为什么当我延迟停止它时(_splashTime) ANR不显示(即使我将其增加到5秒以上)

解决方案

据了解,处理程序正在主线程中运行.

对象无法在线程上运行,因为对象无法运行.方法运行.

但是它具有正在其他线程中运行的线程.

您尚未发布任何涉及其他线程"的代码.上面的代码清单中的所有内容都与流程的主应用程序线程绑定.

MainMenu.class将在主线程还是第二个线程中运行?

对象无法在线程上运行,因为对象无法运行.方法运行. MainMenu似乎是Activity.活动生命周期方法(例如onCreate())在主应用程序线程上调用.

为什么当我延迟(_splashTime)停止它时,ANR不显示(即使我将其增加到5秒以上)

您不是在延迟延迟[主应用程序线程]".您已安排Runnable在延迟_splashTime毫秒后在主应用程序线程上运行.但是,postDelayed()不是阻止呼叫.它只是将一个事件放在事件队列中,该事件将在_splashTime毫秒内不执行.

此外,由于postDelayed()不使用Thread,请用Runnable替换Thread.您的代码可以编译并运行,因为Thread实现了Runnable,但是您会认为使用Thread而不是Runnable会使您感到困惑,这意味着您的代码将在后台线程上运行,而不会在后台线程上运行.

I have following code.

public class SplashScreen extends Activity {
    private int _splashTime = 5000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                 WindowManager.LayoutParams.FLAG_FULLSCREEN);

        new Handler().postDelayed(new Thread(){
           @Override
           public void run(){
             Intent mainMenu = new Intent(SplashScreen.this, MainMenu.class);
             SplashScreen.this.startActivity(mainMenu);
             SplashScreen.this.finish();
             overridePendingTransition(R.drawable.fadein, R.drawable.fadeout);
           }
        }, _splashTime);
    }
}

I have problem in analyze of this code. As far as know handler is running in main thread. but it has thread which is running in other thread.

MainMenu.class will be run in main thread or second thread? if main thread is stopped for 5 seconds ANR will be raise. Why when I am stopping it with delay (_splashTime) ANR doesn't display (even if i increase it to more than 5 seconds)

解决方案

As far as know handler is running in main thread.

Objects do not run on threads, because objects do not run. Methods run.

but it has thread which is running in other thread.

You have not posted any code which involves any "other thread". Everything in the code listing above is tied to the main application thread of your process.

MainMenu.class will be run in main thread or second thread?

Objects do not run on threads, because objects do not run. Methods run. MainMenu appears to be an Activity. The activity lifecycle methods (e.g., onCreate()) are called on the main application thread.

Why when I am stopping it with delay (_splashTime) ANR doesn't display (even if i increase it to more than 5 seconds)

You are not "stopping [the main application thread] with delay". You have scheduled a Runnable to be run on the main application thread after a delay _splashTime milliseconds. However, postDelayed() is not a blocking call. It simply puts an event on the event queue that will not be executed for _splashTime milliseconds.

Also, please replace Thread with Runnable, since postDelayed() does not use Thread. Your code compiles and runs, because Thread implements Runnable, but you will confuse yourself by thinking that using Thread instead of Runnable means that your code will run on a background thread, and it will not.

这篇关于Android,Handler是在主线程还是其他线程中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆