2012-11-05 19 views
0

我想發送一個參數作爲參數,我想開始這個類,因爲這個asynctask想用來啓動不同的活動。從AsyncTask發送類作爲參數啓動intent

例子:

.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
    new taskIntent().execute(**example1.class**);  
} 
} 

.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
    new taskIntent().execute(**example2.class**);  
} 
} 

private class taskIntent extends AsyncTask<String, Integer, Void> { 
    ProgressDialog dialog; 

    @Override 
    protected Void doInBackground(String... params) { 

     return null; 
    } 

    @Override 
    protected void onPreExecute() { 
     dialog = ProgressDialog.show(Calendar.this, "", 
       "Loading...", true); 
     dialog.show(); 
    } 
    protected void onPostExecute(Void unused){ 
     Intent intent = new Intent(Calendar.this, **parameter[0]**); 
     startActivity(intent); 
     finish(); 

     dialog.dismiss(); 
    } 

} 

我怎麼能發送類作爲參數在意向使用它?

謝謝,我的英語不好

+1

for one,Class!= String – njzk2

+0

另外,考慮到你的doInBackground什麼都不做,我沒有看到AsyncTask的要點 – njzk2

+0

我需要Asynctask使用進度對話框直到它加載其他類 – jlopez

回答

2

我不知道使用此一AsyncTask你的方法,但你可以通過一個Class到構造函數:

private class taskIntent extends AsyncTask<String, Integer, Void> { 
    ProgressDialog dialog; 
    Class<?> clazz; 
    ... 
    public taskIntent(Class<?> clazz){ 
     this.clazz = clazz; 
    } 
    ... 
    protected void onPostExecute(Void unused){ 
     Intent intent = new Intent(Calendar.this, clazz); 
     ... 
    } 
} 

然後使用它:

new taskIntent(Example1.class).execute(); 

請注意, Java約定是以類名開頭的大寫字母,所以TaskIntent不是taskIntent

+0

對我來說這是一個令人難以置信的迴應,完美的作品。許多人應該向你學習 – jlopez

1

抱歉,你必須從參數字符串改爲類。儘管如果它沒有做任何事情,我不明白AsyncTask的意義。

+0

謝謝你的迴應。怎麼可能把這個類的名字作爲一個參數發送出去,然後在Asynctask中把它用在意圖中呢? – jlopez