如何获取异步任务的Android结果 [英] How to get results of Async task in Android
问题描述
我想的AsyncTask
。我不明白怎么回事的result.I我也困惑于 doInBackground()
的返回
键入虚空
。为什么它需要返回NULL
;如果我返回NULL
我将如何从这种方法获得价值。
包com.example.shikkok_services;
进口android.app.Dialog;
进口android.content.Context;
进口android.os.AsyncTask;
进口android.os.Handler;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.Window;
进口android.widget.Button;
进口android.widget.ProgressBar;
进口android.widget.TextView;
进口android.widget.Toast;
公共类MyTask的扩展AsyncTask的<太虚,整型,太虚> {
//进度全息UI TE卡伊·科雷泰语境dorkar泰MyTask的èconstrutor decler korlam.Inner类孔瑛太dorkar仓呐
上下文语境;
处理程序处理程序;
对话对话框;
TextView的txtprogrss;
进度进展情况;
按钮btnCancel;
MyTask的(上下文的背景下,处理程序处理){
this.context =背景;
this.handler =处理程序;
}
MyTask的(上下文的背景下){
this.context =背景;
this.handler =处理程序;
}
// --------------------------------在preExecute()......... ...................................
@覆盖
在preExecute保护无效(){
super.on preExecute();
//创建对话框
对话框=新的对话框(背景);
dialog.setCancelable(真正的);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.pogressdialog);
dialog.show();
txtprogrss =(TextView中)dialog.findViewById(R.id.txtProgress);
进度=(进度)dialog.findViewById(R.id.progressBar2);
btnCancel =(按钮)dialog.findViewById(R.id.btnProgress);
//进度=新进度(上下文);
btnCancel.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
MyTask.this.cancel(真正的);
dialog.dismiss();
}
});
dialog.show();
}
// -------------------------------- doInBackground()............. ..............
@覆盖
保护无效doInBackground(虚空......为arg0){
//卡伊HOBE backgournd E不UIË
//做任务和升级味精
的for(int i = 0;我小于10;我++){
如果(isCancelled()){
打破;
}其他{
Log.e(以背景,当前值;+ I);
publishProgress(ⅰ);
//城东条publishProgress()调用HOBE TOTO酒吧OnProgressUpdate HOBE ..
尝试 {
视频下载(500);
}赶上(InterruptedException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
返回null;
}
// ------------------------------ onProgressUpdate()............... ......................
@覆盖
保护无效onProgressUpdate(整数...值){
// dowonload洞江东%的hosse SEITA dhore dhore UI TE dekhalte parbo
super.onProgressUpdate(值);
//更新对话框进展
progress.setProgress(值[0]);
txtprogrss.setText(最新进展+值[0] +%);
}
// ----------------------------- OonPostExecute()................ ...........................
@覆盖
保护无效onPostExecute(无效的结果){
// jokhon卡伊SES锄头jabe tokhon EI方法e asben那么UI TE胆asben
super.onPostExecute(结果);
// handler.sent消息
dialog.dismiss();
Toast.makeText(背景下,已完成,Toast.LENGTH_LONG).show();
}
}
我也搞不清doInBackground是void返回类型。为什么它需要返回null;如果我返回null如何从这个method.please值告诉我。
这是因为那是你已经做到了。您可以返回
你只需要改变的类定义前一种类型。
公共类MyTask的扩展AsyncTask的<太虚,整数,字符串> {//改变过去的参数
通过这个, doInBackground()
将有望返回
A 字符串
键入 onPostExecute()
。这意味着你也有一个方法来改变,以
@覆盖
保护无效onPostExecute(字符串结果){
您会那么显然改变你的返回
语句 doInBackground()
到返回someString
。
我用字符串
作为一个例子,但你可以改变这一点。
至于返回值的AsyncTask完成后,
<一个href="http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648">See有关使用介面这个答案。这是很容易实现,当任务完成后会更新你的活动
。
I am trying AsyncTask
. I cannot understand how to return the result.I am also confused on doInBackground()
has return
type Void
. Why does it need return null
; if I return null
how will I get value from this method.
package com.example.shikkok_services;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MyTask extends AsyncTask<Void, Integer, Void> {
//progressBar holo UI te kaj kore tai Context dorkar tai MyTask e construtor decler korlam.Inner class hole eita dorkar silo na
Context context;
Handler handler;
Dialog dialog;
TextView txtprogrss;
ProgressBar progress;
Button btnCancel;
MyTask(Context context, Handler handler){
this.context=context;
this.handler=handler;
}
MyTask(Context context){
this.context=context;
this.handler=handler;
}
//--------------------------------onPreExecute()............................................
@Override
protected void onPreExecute() {
super.onPreExecute();
// create dialog
dialog=new Dialog(context);
dialog.setCancelable(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.pogressdialog);
dialog.show();
txtprogrss=(TextView) dialog.findViewById(R.id.txtProgress);
progress=(ProgressBar)dialog.findViewById(R.id.progressBar2);
btnCancel=(Button)dialog.findViewById(R.id.btnProgress);
//progress=new ProgressBar(context);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyTask.this.cancel(true);
dialog.dismiss();
}
});
dialog.show();
}
//--------------------------------doInBackground()...........................
@Override
protected Void doInBackground(Void... arg0) {
//Kaj hobe backgournd e not UI e
// do tasks and update msg
for (int i = 0; i < 10; i++) {
if(isCancelled()){
break;
}else{
Log.e("In Background","current value;"+ i);
publishProgress(i);
//joto bar publishProgress() call hobe toto bar OnProgressUpdate hobe..
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
//------------------------------onProgressUpdate().....................................
@Override
protected void onProgressUpdate(Integer... values) {
//dowonload hole koto percent hosse seita dhore dhore UI te dekhalte parbo
super.onProgressUpdate(values);
// update dialog progress
progress.setProgress(values[0]);
txtprogrss.setText("progress update"+ values[0]+"%");
}
//-----------------------------OonPostExecute()...........................................
@Override
protected void onPostExecute(Void result) {
// jokhon kaj ses hoe jabe tokhon ei method e asben then UI te chole asben
super.onPostExecute(result);
// handler.sent message
dialog.dismiss();
Toast.makeText(context, "Finished", Toast.LENGTH_LONG).show();
}
}
I am also confuse doInBackground is return type Void .Why it is need return null; if I return null how get value from this method.please tell me.
This is because that is what you have made it. You can return
a type you just have to change the class definition ex.
public class MyTask extends AsyncTask<Void, Integer, String> { // changed last param
With this, doInBackground()
will be expected to return
a String
type to onPostExecute()
. Which means you would also have to change that method to
@Override
protected void onPostExecute(String result) {
You would then obviously change your return
statement in doInBackground()
to return someString
.
I used String
as an example but you could change that.
As far as returning value after AsyncTask finishes
See this answer about using an interface. It is really easy to implement and will update your Activity
when the task finishes.
Also please read (or re-read) the AsyncTask docs
这篇关于如何获取异步任务的Android结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!