2015-12-18 124 views
0

我一直在嘗試實現AsyncTask,但沒有成功。使用AsyncTask問題

有一個活動假設在複製所有文件時顯示加載進度,然後調用主要活動。

這是我LoadingActivity代碼:

package br.com.moon.light; 

import android.content.Intent; 
import android.content.res.AssetManager; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

public class LoadingActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_loading); 
     copyAssets(); 
     Intent intent = new Intent(LoadingActivity.this,MainActivity.class); 
     startActivity(intent); 
     finish(); 
    } 

    private void copyAssets() { 
     AssetManager assetManager = getAssets(); 
     String[] files = null; 
     try { 
      files = assetManager.list(""); 
     } catch (IOException e) { 
      Log.e("tag", "Failed to get asset file list.", e); 
     } 
     if (files != null) for (String filename : files) { 
      InputStream in = null; 
      OutputStream out = null; 
      try { 
       in = assetManager.open(filename); 
       File outFile = new File(getExternalFilesDir(null), filename); 
       out = new FileOutputStream(outFile); 
       copyFile(in, out); 
      } catch(IOException e) { 
       Log.e("tag", "Failed to copy asset file: " + filename, e); 
      } 
      finally { 
       if (in != null) { 
        try { 
         in.close(); 
        } catch (IOException e) { 
         // NOOP 
        } 
       } 
       if (out != null) { 
        try { 
         out.close(); 
        } catch (IOException e) { 
         // NOOP 
        } 
       } 
      } 
     } 
    } 
    private void copyFile(InputStream in, OutputStream out) throws IOException { 
     byte[] buffer = new byte[1024]; 
     int read; 
     while((read = in.read(buffer)) != -1){ 
      out.write(buffer, 0, read); 
     } 
    } 

} 

在此先感謝。

EDITED

我提高了我的代碼,但我還是不明白爲什麼程序加載時的佈局不顯示..

這裏是的.java .xml文件:

LoadingActivity.java

package br.com.moon.light; 

import android.content.Intent; 
import android.content.res.AssetManager; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.widget.ProgressBar; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

public class LoadingActivity extends AppCompatActivity { 

    private ProgressBar progressBar; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_loading); 
     Intent intent = new Intent(LoadingActivity.this,MainActivity.class); 
     progressBar = (ProgressBar) findViewById(R.id.progressBar); 
     progressBar.setMax(10); 
     startActivity(intent); 
     Copyfiles(); 
    } 

    private class Tasks extends AsyncTask<Void, Integer, Void>{ 
     @Override 
     protected Void doInBackground(Void... params) { 
      copyAssets(); 
      return null; 
     } 
     protected void OnPostExecute(Void param) { 
      finish(); 
     } 
     @Override 
     protected void onProgressUpdate(Integer... values){ 
      progressBar.setProgress(values[0]); 
     } 
    } 

    private void copyAssets() { 
     AssetManager assetManager = getAssets(); 
     String[] files = null; 
     try { 
      files = assetManager.list(""); 
     } catch (IOException e) { 
      Log.e("tag", "Failed to get asset file list.", e); 
     } 
     if (files != null) for (String filename : files) { 
      InputStream in = null; 
      OutputStream out = null; 
      try { 
       in = assetManager.open(filename); 
       File outFile = new File(getExternalFilesDir(null), filename); 
       out = new FileOutputStream(outFile); 
       copyFile(in, out); 
      } catch(IOException e) { 
       Log.e("tag", "Failed to copy asset file: " + filename, e); 
      } 
      finally { 
       if (in != null) { 
        try { 
         in.close(); 
        } catch (IOException e) { 
         // NOOP 
        } 
       } 
       if (out != null) { 
        try { 
         out.close(); 
        } catch (IOException e) { 
         // NOOP 
        } 
       } 
      } 
     } 
    } 
    private void copyFile(InputStream in, OutputStream out) throws IOException { 
     byte[] buffer = new byte[1024]; 
     int read; 
     while((read = in.read(buffer)) != -1){ 
      out.write(buffer, 0, read); 
     } 
    } 

    private void Copyfiles(){ 
     new Tasks().execute(); 
    } 
} 

activity_loading.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".LoadingActivity"> 

    <ProgressBar 
     android:id="@+id/progressBar" 
     android:minHeight="20dip" 
     android:maxHeight="20dip" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     style="@android:style/Widget.ProgressBar.Horizontal" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView" 
     android:layout_centerHorizontal="true" 
     android:src="@mipmap/ic_launcher" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="159dp" /> 

</RelativeLayout> 
+0

你的問題是什麼? – LaurentY

+0

我已更新完整的代碼...該應用程序以空白屏幕開始,然後顯示主要活動...如果我按後退按鈕,它顯示該死的進度條。我想要有進度條或至少一個文本通知程序正在加載,而它在後臺複製數百個PDF文件。 – Borsatto

回答

0
private class Tasks extends AsyncTask<Void, Integer, Void>{ 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(LoadingActivity.this); 
     pDialog.setMessage("Loading Files. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     copyAssets(); 
     return null; 
    } 
    protected void OnPostExecute(Void param) { 
     // dismiss the dialog after getting all files 
     pDialog.dismiss(); 
     finish(); 
    } 
} 

注:聲明

私人ProgressDialog pDialog;

0

你有一個奇怪的東西,爲什麼你在onCreate方法調用MainActivitystartActivity

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_loading); 
     Intent intent = new Intent(LoadingActivity.this,MainActivity.class); 
     progressBar = (ProgressBar) findViewById(R.id.progressBar); 
     progressBar.setMax(10); 
     startActivity(intent); 
     Copyfiles(); 
    }