2015-12-31 30 views
0

我想实现进度条,而我的方法LlamadoServicio在后台执行。正确执行线程与锁定屏幕

我需要使用我的progressDialog锁定屏幕并隐藏此元素才能完成“LlamadoServicio()”。

enter image description here

我的方法

progress = ProgressDialog.show(Menu.this, null, null, true); 
progress.setContentView(R.layout.elemento_progress_dialog); 

new Thread(new Runnable() { 
    public void run() { 

    LlamadoServicio("david"); 

    mHandler.post(new Runnable() { 
    public void run() { 
    progress.dismiss(); 
    } 
    });  
    } 
}).start(); 
+0

你的问题到底是什么?代码看起来合法 – ligi

+0

而不是'new Thread(new Runnable()...'需要尝试[AsyncTask](http://developer.android.com/reference/android/os/AsyncTask。html) – pRaNaY

+0

不确定你想实现什么?无论如何,你的代码越野车。它不建议在线程中使用处理程序。 – dhams

回答

5

您可以使用异步任务这一点。

private class AsyncTaskSample extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected Void doInBackground(Void) { 
     LlamadoServicio("david"); 
    } 


    @Override 
    protected void onPostExecute(Void) { 
     progress.dismiss(); 
    } 


    @Override 
    protected void onPreExecute() { 
    // Things to be done before execution of long running operation. For 
    // example showing ProgessDialog 
     progress = ProgressDialog.show(Menu.this, null, null, true); 
     progress.setContentView(R.layout.elemento_progress_dialog); 
     progress.show(); 
    } 

} 

下面是关于android中的异步任务的几个很好的参考。

http://codetheory.in/android-asynctask/

http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html#.Vo87yfl97IU

+0

此编码有错误!但它类似于正确的答案 –

+1

@DavidHackro此代码解决您的问题,这应该被标记为最终答案 –

2

你的代码需要做的是,它会显示一个进度对话框,并开始通过调用LlamadoServicio("david")做的工作。同时,处理程序将忽略进度对话框。

有做这两种方法:

1)使用AsyncTask,在onPreExecute()功能显示进度对话框,叫LlamadoServicio("david")doInBackground功能和onPostExecute功能解散你的进度对话框。

2)如果你仍然想使用ThreadHandler,你需要如下更改代码:

progress = ProgressDialog.show(Menu.this, null, null, true); 
progress.setContentView(R.layout.elemento_progress_dialog); 

new Thread(new Runnable() { 
    public void run() { 

    LlamadoServicio("david"); 

    } 
}).start(); 

LlamadoServicio代码:

void LlamadoServicio(String value) { 
// do your job 
mHandler.sendEmptyMessage(0); 

} 

处理器代码:

Handler mHandler = new Handler(new Handler.Callback() { 
      @Override 
      public boolean handleMessage(Message msg) { 
       progress.dismiss(); 
       return false; 
      } 
     }); 
0

正确执行线程

private class MyAsyncClass extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 

     progress = ProgressDialog.show(RegistroAuto.this, null, null, true); 
     progress.setContentView(R.layout.elemento_progress_dialog); 
     progress.show(); 
     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     autosConsulta = LlamadoServicio(placa); 
     if (autosConsulta != null) { 
      autosConsulta.setSerie(serie.replaceAll(" ","").trim()); 
      hp.nuevoAuto(autosConsulta); 
      startActivity(new Intent(RegistroAuto.this, PrincipalAutos.class)); 
      finish(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     progress.dismiss(); 
     finish(); 
     super.onPostExecute(result); 
    } 
} 
+0

我爱西班牙/意大利的编码器。没有什么比混合使用英语和其他语言更好 – dominik4142

2

如果需要在你的代码是在后台运行锁屏,我个人觉得你应该在这种情况下,使用异步任务。

private class MyAsyncTask extends AsyncTask<Void, Void, Void> { 

    protected Void doInBackground(Void) { 
    /* do your background work */ 
    } 

    @Override 
    protected void onPostExecute(Void) { 
    progress.dismiss(); 
    } 


    @Override 
    protected void onPreExecute() { 
    // Things to be done before execution of long running operation. For 
    // example showing ProgessDialog 
    progress = ProgressDialog.show(Menu.this, null, null, true); 
    progress.setContentView(R.layout.elemento_progress_dialog); 
    // use this if you want to lock the screen. 
    progress.setCancelable(false); 
    progress.show(); 
    } 
} 

2.你正在做它的方式是错误的,因为在run()方法,你会给呼叫服务调用方法:

LlamadoServicio("david"); 

比方说你的服务启动,但这一呼吁不会阻止处理程序发布可运行。因此它会驳回进度条。