如何将一个parameter从一个方法发送到另一个android



hi我有一个用于从url下载文件的DownloadFileAsync类我从网上完美下载文件,但下载完成后,我想从系统播放mp3文件,但我无法将地址文件从doInBackground发送到onPostExecute
我的代码是:

 protected String doInBackground(String... aurl) {
         try {
             File root = Environment.getExternalStorageDirectory();
             URL u = new URL(aurl[0]);
             HttpURLConnection c = (HttpURLConnection) u.openConnection();
             c.setRequestMethod("GET");
             c.setDoOutput(true);
             c.connect();
             int lenghtOfFile = c.getContentLength();
             File myFolder = new File( root + "/download/"+aurl[1]+"/" );
             myFolder.mkdir();
             FileOutputStream f = new FileOutputStream(new File(root + "/download/"+aurl[1]+"/", aurl[2]+".mp3"));
             InputStream in = c.getInputStream();
             byte[] buffer = new byte[512];
             int len1 = 0;
             long total = 0;
             while ((len1 = in.read(buffer)) > 0) {
                 total += len1; //total = total + len1
                 publishProgress("" + (int)((total*100)/lenghtOfFile));
                 f.write(buffer, 0, len1);
             }
             f.close();
         } catch (Exception e) {
             Log.d("Downloader", e.getMessage());
         }
         return null;
     }

onPostExecute代码为:

 protected void onPostExecute(String unused) {
         mProgressDialog.dismiss();
         File musicFile2Play = new File("/sdcard/download/بابالنگ دراز/شماره یک.mp3");
         Intent i2 = new Intent();
         i2.setAction(android.content.Intent.ACTION_VIEW);
         i2.setDataAndType(Uri.fromFile(musicFile2Play), "audio/mp3");
         context.startActivity(i2);
        // intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "hamrahtest.apk")), "application/vnd.android.package-archive");
     }

如何将aurl[1]和aurl[2]从doInBackground发送到onPostExecute文件名?

您应该用您希望OnPostExecute需要的对象填充doInBackground的返回。例如:

protected List<String> doInBackground(String... aurl) {
     List<String> list = new ArrayList<String>();
     try {
         File root = Environment.getExternalStorageDirectory();
         ...
         list.add(aurl[0]);
         list.add(aurl[1]);
         list.add(aurl[2]);
         list.add(aurl[3]);
     } catch (Exception e) {
         Log.d("Downloader", e.getMessage());
     }
     return list;
 }
protected void onPostExecute(List<String> list) {
     mProgressDialog.dismiss();
     for (String url : list) {
        // Do you handling here
     }
 }

你应该这样声明你的AsyncTask

public class MyAsyncTask extends AsyncTask<Void, Void, List<String>> {

第一个:

DownloadFileAsync extends AsyncTask<String, String, String[]>  

第二次更改

protected String doInBackground(String... aurl) {  

protected String[] doInBackground(String... aurl) {
.....
......
return aurl;
}  

第三次更改

protected void onPostExecute(String unused)  

protected void onPostExecute(String[] aurl) {

一个简单的解决方案是在AsyncTask类上生成这些实例变量。在doInBackground中设置它们,在onPostExecute()中读取它们。

最新更新