2014-06-18 67 views
0

我想從片段中調用其他一些活動中的異步任務。我試圖用各種方式打電話,但都沒有成功。我只是想知道什麼叫靜態的AsyncTask。這裏最好的辦法就是我的異步任務:從其他片段調用靜態異步任務

static class MyAsync extends AsyncTask<Void, Void, Void> { 
    Context context; 
    String username, password; 
    private MyAsync(Context context, String username, String password) { 
     this.context = context; 
     this.username = username; 
     this.password = password; 
    } 
    ProgressDialog dialog; 
    private String response; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     dialog = ProgressDialog.show(context, "Connecting to Server","Getting Credentials" 
       , true); 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) {  
     try {    
       ContentDownload download = new ContentDownload(); 
       response = download.loginApi(agentId, password); 
       if(response.contains("Success")){ 
        if(SettingHelper.getFirstCall(context)){ 
         ContentDownload.CallApi(context); 
         SettingHelper.setFirstCall(context, false); 
        } 
        if(SettingHelper.getFirstLaunch(context)){ 
         ContentDownload load = new ContentDownload(); 
         load.callItemApi(context); 
         load.callActionApi(context); 
         SettingHelper.setFirstLaunch(context, false); 
        } 
      } 
     } catch (Exception e) { 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     if(response.contains("Success")){ 
     context.startActivity(new Intent(context, AllActivity.class)); 
     }else{ 
      Toast.makeText(context, "Got back", Toast.LENGTH_SHORT).show(); 
     } 
    dialog.dismiss(); 
}} 

我想這樣調用它:

 LoginActivity.new MyAsync(getActivity).execute(); 

但它給錯誤

+0

你把它稱爲完全相同的方式,爲什麼會有差異?我建議你在嘗試編寫應用程序之前先學習java。試試這個:'new LoginActivity.MyAsync(getActivity())。execute();' –

+0

謝謝..我的錯誤..謝謝@XaverKapeller – user3740085

回答

0

它要使用從Fragment這個類,給它的公共知名度,也是一個公共構造函數,然後你可以把它叫做:

new LoginActivity.MyAsync(getActivity())