我有以下代码。
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
毫秒执行。
另外,请用Runnable
替换Thread
,因为postDelayed()
不使用Thread
。您的代码编译和运行,因为Thread
实现了Runnable
,但是您会通过认为使用Thread
而不是Runnable
来混淆自己,这意味着您的代码将在背景线程上运行,并且它不会。