Android 完成线程后关闭对话框
我正在android中为一个耗时的操作创建一个线程。我希望主屏幕显示一个进度对话框,其中包含一条消息,通知操作正在进行,但我希望该对话框在线程完成后关闭。我尝试了join,但它会锁定线程,并且不会显示对话框。我尝试使用:Android 完成线程后关闭对话框,android,multithreading,Android,Multithreading,我正在android中为一个耗时的操作创建一个线程。我希望主屏幕显示一个进度对话框,其中包含一条消息,通知操作正在进行,但我希望该对话框在线程完成后关闭。我尝试了join,但它会锁定线程,并且不会显示对话框。我尝试使用: dialog.show(); mythread.start(); dialog.dismiss(); 但是对话框没有显示出来。如何在不锁定主线程的情况下创建该序列而等待线程结束 就我所知: public class syncDataElcanPos extends Asyn
dialog.show();
mythread.start();
dialog.dismiss();
但是对话框没有显示出来。如何在不锁定主线程的情况下创建该序列而等待线程结束
就我所知:
public class syncDataElcanPos extends AsyncTask<String, Integer, Void> {
ProgressDialog pDialog;
Context cont;
public syncDataElcanPos(Context ctx) {
cont=ctx;
}
protected void onPreExecute() {
pDialog = ProgressDialog.show(cont,cont.getString(R.string.sync), cont.getString(R.string.sync_complete), true);
}
protected Void doInBackground(String... parts) {
// blablabla...
return null;
}
protected void onProgressUpdate(Integer... item) {
pDialog.setProgress(item[0]); // just for possible bar in a future.
}
protected void onPostExecute(Void unused) {
pDialog.dismiss();
}
公共类syncDataElcanPos扩展异步任务{
ProgressDialog;
语境控制;
公共syncDataElcanPos(上下文ctx){
cont=ctx;
}
受保护的void onPreExecute(){
pDialog=ProgressDialog.show(cont,cont.getString(R.string.sync),cont.getString(R.string.sync_complete),true);
}
受保护的空doInBackground(字符串…部分){
//呜呜呜呜。。。
返回null;
}
受保护的void onProgressUpdate(整数…项){
pDialog.setProgress(项[0]);//仅供将来使用。
}
受保护的void onPostExecute(未使用的void){
pDialog.disclose();
}
但是当我尝试执行它时,它会给我一个异常:“无法添加窗口”。在执行后的操作中,您可以调用dismise
下面是另一个线程的示例
class AddTask extends AsyncTask<Void, Item, Void> {
protected void onPreExecute() {
pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);
}
protected Void doInBackground(Void... unused) {
items = parser.getItems();
for (Item it : items) {
publishProgress(it);
}
return(null);
}
protected void onProgressUpdate(Item... item) {
adapter.add(item[0]);
}
protected void onPostExecute(Void unused) {
pDialog.dismiss();
}
}
class AddTask扩展了AsyncTask{
受保护的void onPreExecute(){
pDialog=ProgressDialog.show(MyActivity.this,“请稍候…”,“正在检索数据…”,true);
}
受保护的空位背景(空位…未使用){
items=parser.getItems();
对于项目(it:项目){
出版进度(it);
}
返回(空);
}
受保护的void onProgressUpdate(项目…项目){
添加(第[0]项);
}
受保护的void onPostExecute(未使用的void){
pDialog.disclose();
}
}
在postexecute中,您可以调用dismise
下面是另一个线程的示例
class AddTask extends AsyncTask<Void, Item, Void> {
protected void onPreExecute() {
pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);
}
protected Void doInBackground(Void... unused) {
items = parser.getItems();
for (Item it : items) {
publishProgress(it);
}
return(null);
}
protected void onProgressUpdate(Item... item) {
adapter.add(item[0]);
}
protected void onPostExecute(Void unused) {
pDialog.dismiss();
}
}
class AddTask扩展了AsyncTask{
受保护的void onPreExecute(){
pDialog=ProgressDialog.show(MyActivity.this,“请稍候…”,“正在检索数据…”,true);
}
受保护的空位背景(空位…未使用){
items=parser.getItems();
对于项目(it:项目){
出版进度(it);
}
返回(空);
}
受保护的void onProgressUpdate(项目…项目){
添加(第[0]项);
}
受保护的void onPostExecute(未使用的void){
pDialog.disclose();
}
}
线程完成后,使用runOnUIThread方法关闭对话框
runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
}
});
线程完成后,使用runOnUIThread方法关闭对话框
runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
}
});
要做到这一点,有两种方法(我更喜欢第二种方法:AsyncTask): 首先:显示您的
警报对话框
,然后在方法run()
上执行此操作
@override
public void run(){
//the code of your method run
//....
.
.
.
//at the end of your method run() , dismiss the dialog
YourActivity.this.runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
}
});
}
Second:使用如下异步任务:
class AddTask extends AsyncTask<Void, Item, Void> {
protected void onPreExecute() {
//create and display your alert here
pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Downloading data ...", true);
}
protected Void doInBackground(Void... unused) {
// here is the thread's work ( what is on your method run()
items = parser.getItems();
for (Item it : items) {
publishProgress(it);
}
return(null);
}
protected void onProgressUpdate(Item... item) {
adapter.add(item[0]);
}
protected void onPostExecute(Void unused) {
//dismiss the alert here where the thread has finished his work
pDialog.dismiss();
}
}
class AddTask扩展了AsyncTask{
受保护的void onPreExecute(){
//在此处创建并显示您的警报
pDialog=ProgressDialog.show(MyActivity.this,“请稍候…”,“下载数据…”,true);
}
受保护的空位背景(空位…未使用){
//以下是线程的工作(方法run()上的内容)
items=parser.getItems();
对于项目(it:项目){
出版进度(it);
}
返回(空);
}
受保护的void onProgressUpdate(项目…项目){
添加(第[0]项);
}
受保护的void onPostExecute(未使用的void){
//在线程已完成其工作的位置解除警报
pDialog.disclose();
}
}
要做到这一点,有两种方法(我更喜欢第二种方法:AsyncTask):
首先:显示您的警报对话框
,然后在方法run()
上执行此操作
@override
public void run(){
//the code of your method run
//....
.
.
.
//at the end of your method run() , dismiss the dialog
YourActivity.this.runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
}
});
}
Second:使用如下异步任务:
class AddTask extends AsyncTask<Void, Item, Void> {
protected void onPreExecute() {
//create and display your alert here
pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Downloading data ...", true);
}
protected Void doInBackground(Void... unused) {
// here is the thread's work ( what is on your method run()
items = parser.getItems();
for (Item it : items) {
publishProgress(it);
}
return(null);
}
protected void onProgressUpdate(Item... item) {
adapter.add(item[0]);
}
protected void onPostExecute(Void unused) {
//dismiss the alert here where the thread has finished his work
pDialog.dismiss();
}
}
class AddTask扩展了AsyncTask{
受保护的void onPreExecute(){
//在此处创建并显示您的警报
pDialog=ProgressDialog.show(MyActivity.this,“请稍候…”,“下载数据…”,true);
}
受保护的空位背景(空位…未使用){
//以下是线程的工作(方法run()上的内容)
items=parser.getItems();
对于项目(it:项目){
出版进度(it);
}
返回(空);
}
受保护的void onProgressUpdate(项目…项目){
添加(第[0]项);
}
受保护的void onPostExecute(未使用的void){
//在线程已完成其工作的位置解除警报
pDialog.disclose();
}
}
谢谢大家的帮助,我将尝试AsyncTask选项,看看它是如何运行的……我如何添加“进度上下文”对话框?MyActivity。这对我不起作用,它说它不能使用这些参数创建,它采用的是活动本身,而不是上下文。是否将类AddTask声明为活动的内部类?如果是的,那么它应该工作,如果没有,那么你应该将上下文传递给你的类AddTask是的,我已经传递给了任务,但是我现在遇到了一个异常,有什么想法吗?代码在主要问题部分。在你的活动中,在onCreate方法中,你应该像这样实例化你的类syncDataElcanPos:syncDataElcanPos instance=new syncDataElcanPos(本);谢谢大家的帮助,我将尝试AsyncTask选项,看看它是如何运行的……我如何添加进度上下文对话框?MyActivity。这对我不起作用,它说它不能用这些参数创建,它接受的是活动本身,而不是上下文。您是否将AddTask类声明为活动的内部类?如果是,t如果不能,那么应该将上下文传递给类AddTask是的,我已经传递给了任务,但是我现在遇到了一个异常,有什么想法吗?代码在主要问题部分。在活动中,在onCreate方法中,应该实例化类syncDataElcanP