在线程内启动新活动

时间:2012-05-01 01:43:33

标签: android

我对线程有疑问。我不确定这里发生了什么。我的理解是,在长期活动结束之前,进度对话框不会结束,但这不是正在发生的事情。我希望long活动开始,完成,关闭微调器,然后以意图开始新活动。

 final ProgressDialog spinnerDialog = ProgressDialog.show(  
                MainScreen.this, "",  
                "Waiting for accurate (> 10 meters) GPS coordinates...Please wait. ", true);  
            new Thread(new Runnable() {  
                public void run() {  
                    //Do something that takes a while
                    spinnerDialog.dismiss();  
                    return;  
                }  
            }).start();

              Intent tvi = new Intent();
              tvi.setClass(getInstance(), TabbedView.class);
              startActivity(tvi);

1 个答案:

答案 0 :(得分:0)

应在主(UI)线程中启动新活动。但只是在启动新线程后编写代码并不能保证您拥有创建意图所需的所有信息。我建议使用处理程序

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    progress = (ProgressBar) findViewById(R.id.progressBar1);
    handler = new Handler();
    startprogress();
}

public void startProgress(View view) {
    // Do something long
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //do long stuff (like getting info for intent)
            handler.post(new Runnable() {
            @Override
            public void run() {
                //make new intent
                //start new activity with intent you just made
            }
        });
        }
    }
    };
    new Thread(runnable).start();
}

}

更重要的是,这是处理程序上的开发人员页面的链接: http://developer.android.com/reference/android/os/Handler.html