我知道這個問題以前有人問,我已經看了:如何通過價值從onPostExecute
How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
how do i send data back from onPostExecute in an AsyncTask?
how to pass the result of asynctask onpostexecute method into the parent activity android
我試圖創建一個接口傳遞值,我也嘗試創建一個方法來通過我的onPostExecute賦值,最後我試圖直接設置下面的變量,但似乎沒有任何工作。
public class ClassName extends AppCompatActivity {
private Bitmap bit;
public void methodName() {
new RetrieveFavIcon().execute("https://www.google.com");
// work with 'bit' value, but bit value is null, why?
}
class RetrieveFavIcon extends AsyncTask<String, Void, Bitmap> {
public TaskListener delegate = null;
protected Bitmap doInBackground(String... urls) {
try {
URL url = new URL(new URL(urls[0].trim()), "/favicon.ico");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
Log.d("URL return", "failed");
e.printStackTrace();
return null;
}
}
protected void onPostExecute(Bitmap bm) {
if(bm != null) {
bit = bm;
Log.d("bitmap", "bitmap not null");
}
}
}
任何解釋這是爲什麼越來越downvotes? –
試試這個,http://android-er.blogspot.de/2013/09/implement-callback-function-with.html –