2016-10-17 40 views
-2

我想在异步任务onPreExecute()中显示进度条。但我无法通过context。有人可以帮忙吗?在Android中的异步任务中显示进度条

public class myClass extends AsyncTask<Void,Void,Void>{ 
    Context ctx; 
    ProgressDialog d; 
    String s; 


    public myClass (String S, Context con) { 
     this.ctx = con; 
     this.s = S; 
    } 


    @Override 
     protected void onPreExecute() { 
      d = new ProgressDialog(this.ctx); 
      d.setMessage("Please wait..."); 
      d.setIndeterminate(true); 
      d.show(); 
     } 

从主要活动作为调用

new myClass(MainActivity.this); 
myClassObj.execute(s); 
+0

你可以使用getApplicationContext()方法为此 –

+0

使用构造函数 –

+1

@RahulKhurana我应该在哪里使用它? – Amar

回答

0

它应该是这样的,传递到构造函数。

public class myClass extends AsyncTask<Void,Void,Void>{ 
    private ProgressDialog dialog; 
    private String paramOne; 
    private int paramTwo; 

    public myClass (Activity activity, String paramOne, int paramTwo) { 
     dialog = new ProgressDialog(activity); 
     this.paramOne = paramOne; // "Hello" 
     this.paramTwo = paramTwo; // 123 
    } 

    @Override 
    protected void onPreExecute() { 
     dialog.setMessage("Please wait..."); 
     dialog.setIndeterminate(true); 
     dialog.show(); 
    } 

这样称呼。

new myClass(YourActivity.this, "Hello!", 123).execute(); 
+0

'new myClassObj(MainActivity.this).execute();'不起作用。 – Amar

+0

试试我的更新回答。 –

+0

无法正常工作。看到我更新的问题。我也不想在异步类中发送参数。同时在异步类中显示接收代码。 – Amar

0

我对此有一个更好的方法,用在你的活动或片段的进度条,一旦你开始你的AsyncTask设置能见度可见,并且当你导致对postExecute设置使用了知名度活动中的界面。事情是这样的

/** 
    * Listener for listening events happening in background task, whether cancelled or completed 
    * with error or success. 
    */ 
    public interface Listener { 
     /** 
     * Callback invoked when request is completed and response is got with 
     * code == {@value java.net.HttpURLConnection#HTTP_OK}. 
     */ 
     void onSuccess(Response response); 

     /** 
     * Callback invoked when request is cancelled or completed with response 
     * code != {@value java.net.HttpURLConnection#HTTP_OK}. 
     */ 
     void onError(Response response); 
    } 

,并在您的活动

void onSuccess(Response response){ 
     //Visibility to Gone. 
    } 
0

会有调用的AsyncTask, 当在同一活动调用的AsyncTask的两种情况: -

public class myClass extends AsyncTask<Void,Void,Void>{ 
ProgressDialog d; 
String s; 

public myClass (String str) { 
    this.s= str; 
} 
@Override 
    protected void onPreExecute() { 
     d = new ProgressDialog(getApplicatinContext()); 
     d.setMessage("Please wait..."); 
     d.setIndeterminate(true); 
     d.show(); 
    } 

从主调用Activity as

myClassObj.execute(); 

如果u需要在不同的活动或单独的文件来调用然后u需要遵循下面的代码: -

public class myClass extends AsyncTask<Void,Void,Void>{ 
    Activity act; 
    ProgressDialog d; 


public myClass (Activity act) { 
    this.act = act; 
} 


@Override 
    protected void onPreExecute() { 
     d = new ProgressDialog(act.getApplicationContext()); 
     d.setMessage("Please wait..."); 
     d.setIndeterminate(true); 
     d.show(); 
    } 

从主要活动调用作为

new myClass(MainActivity.this); 
myClassObj.execute(s); 
+0

查看我更新的问题。我想发送参数给异步类。 – Amar

+0

我已编辑的代码请让我知道如果你有这个麻烦.. – santhoshkumar