从AsyncTask返回结果

时间:2012-09-01 08:28:39

标签: android android-asynctask

public int postToTwitter(String msg) {
    String url = null;

    new ImageSender().execute();
    // Twitlink I have to pass in next line but it showing null
    twitter.updateStatus(msg+" "+twitlink);

    return 1;
}

AsyncTask类:

private class ImageSender extends AsyncTask<URL, Integer, Long> {
    private String url;

    protected void onPreExecute() {
        mProgressDialog = ProgressDialog.show(OutgoingHome.this, "", "Sending image...", true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    protected Long doInBackground(URL... urls) {            
        long result = 0;

        ImageUpload upload = ImageUpload.getTwitpicUploader ("8b8f5db5ca383798509af8459f968dbc",auth);
        try {
            twitlink = upload.upload(new File(path));
            result = 1;
        } catch (Exception e) {        

        }

        return result;
    }

    protected void onPostExecute(Long result) {
        mProgressDialog.cancel();
    }
}

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用get()的{​​{1}}方法。另一种方法是实现AsyncTask并在OnResultListener onResult()内调用其AsyncTask方法。但是,第一个解决方案应该足够了。希望这会有所帮助。