Java 用锁屏正确实现线程
我试图在后台执行我的方法LlamadoServicio时实现progressbar 我需要在progressDialog中锁定屏幕并隐藏此元素以完成此过程 我的方法Java 用锁屏正确实现线程,java,android,multithreading,Java,Android,Multithreading,我试图在后台执行我的方法LlamadoServicio时实现progressbar 我需要在progressDialog中锁定屏幕并隐藏此元素以完成此过程 我的方法 您可以为此使用异步任务 private class AsyncTaskSample extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void) { LlamadoServicio("david"
您可以为此使用异步任务
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中异步任务的好参考
您的代码将要做的是,它将显示一个进度对话框,并将通过调用LlamadoServiciodavid开始工作。同时,处理程序将关闭进度对话框 有两种方法可以做到这一点: 1使用AsyncTask,在onPreExecute函数中显示进度对话框,在doInBackground函数中调用LlamadoServiciodavid,并在onPostExecute函数中关闭进度对话框 2如果仍要使用线程和处理程序,则需要按以下方式更改代码:
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代码:
处理程序代码:
正确的实现线程
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);
}
}
如果您需要在代码在后台运行时锁定屏幕,我个人认为您应该在这种情况下使用异步任务
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();
}
}
假设您的服务已启动,但此调用不会阻止处理程序发布runnable。因此,它将关闭进度条 你到底有什么问题?代码看起来合法,而不是新线程new Runnable。。。需要尝试但不确定你想要实现什么?不管怎样,你的代码有问题。不建议在线程内部使用处理程序。@mpromonet抱歉,我发布了我的答案:D@DavidHackro:谢谢,这是更不可理解的,即使它可以有一点更好的解释这段代码有错误!但这是正确的answer@DavidHackro此代码解决了您的问题,应标记为最终答案我喜欢西班牙/意大利编码器。没有什么比英语和其他语言的混合更好了
Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
progress.dismiss();
return false;
}
});
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);
}
}
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();
}
}
LlamadoServicio("david");