Android在活动中调用异步任务

And*_*ice 9 android class android-asynctask android-activity

我有一个活动,从gridview中选择一个图像,它允许您保存图像.我正在为我的所有代码使用异步任务.我从几个类中分离了我的AsyncTask.我如何通过我的活动给他们打电话?如何将字符串传递回AsyncTask.

SingleImageView.class

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.save_image:
                new SaveImageTask().execute(image_url,context); //<-- The method execute(String...) in the type AsyncTask<String,String,String> is not applicable for the arguments (String, Context)

                  return true;
            default:
                return false;
        }
Run Code Online (Sandbox Code Playgroud)

SaveImageTask.class

 public class SaveImageTask extends AsyncTask<String, String, String>
    {
        private Context context;
        private ProgressDialog pDialog;
        String image_url;
        URL myFileUrl = null;
        Bitmap bmImg = null;
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub

            super.onPreExecute();

            pDialog = new ProgressDialog(context);  //<<-- Couldnt Recognise
            pDialog.setMessage("Downloading Image ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub

            try {  
                myFileUrl = new URL(image_url);
                HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();   
                conn.setDoInput(true);   
                conn.connect();     
                InputStream is = conn.getInputStream();
                bmImg = BitmapFactory.decodeStream(is); 
            }
            catch (IOException e)
            {       
                e.printStackTrace();  
            }
            try {       

                String path = myFileUrl.getPath();
                String idStr = path.substring(path.lastIndexOf('/') + 1);
            File filepath = Environment.getExternalStorageDirectory();
            File dir = new File (filepath.getAbsolutePath() + "/Wallpaper/");
                dir.mkdirs();
                String fileName = idStr;
                File file = new File(dir, fileName);
                FileOutputStream fos = new FileOutputStream(file);
                bmImg.compress(CompressFormat.JPEG, 75, fos);   
                fos.flush();    
                fos.close();       
            }
            catch (Exception e)
                    {
                        e.printStackTrace();  
                    }
            return null;   
        }
        @Override
        protected void onPostExecute(String args) {
            // TODO Auto-generated method stub

            pDialog.dismiss();

        }
    }
Run Code Online (Sandbox Code Playgroud)

Tyl*_*eat 16

您创建一个新实例,SaveImageTask然后调用其execute方法,将String参数传递给它(execute采用varargs).

new SaveImageTask().execute("foo", "bar");
Run Code Online (Sandbox Code Playgroud)

编辑

由于您AsyncTask使用a Context,因此需要通过构造函数传递它.

public class SaveImageTask extends AsyncTask<String, String, String>
{
    private Context context;
    private ProgressDialog pDialog;
    String image_url;
    URL myFileUrl = null;
    Bitmap bmImg = null;

    public SaveImageTask(Context context) {
        this.context = context;
    }

    ...

}
Run Code Online (Sandbox Code Playgroud)

然后AsyncTask从你的Activity喜欢中拨打电话:

new SaveImageTask(this).execute("foo", "bar");
Run Code Online (Sandbox Code Playgroud)