我认为您应该需要自定义回调,以便在所有下载任务完成后触发asynctask。以下是修改后的代码:
public class SplashScreen extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
File folder = new File(Environment.getExternalStorageDirectory() + "/"+getString(R.string.folder_name));
if(!folder.exists()) {
folder.mkdir();
}
new DataSyncFb(this, new DataSyncFb.OnTaskFinishedCallback(
@Override
public void onTaskFinished() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
)).execute();
}
public class DataSyncFb extends AsyncTask<Void, Integer, Void> {
// Custom callback
public interface OnTaskFinishedCallback {
public void onTaskFinished();
}
private Context context;
private StorageReference mStorageRef;
private OnTaskFinishedCallback callback;
public DataSyncFb(final Context context, OnTaskFinishedCallback callback) {
this.context = context;
this.callback = callback;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i("DataSincFB", "onPreExecute");
}
@Override
protected Void doInBackground(Void... voids) {
final DatabaseHandler db = new DatabaseHandler(context);
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dbRef = database.getReference("categorie");
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
List<String> listCat = db.getAllCategoriesId();
List<String> listSounds = db.getAllSoundsId();
Log.i("test", String.valueOf(dataSnapshot.getValue()));
Gson test = new Gson();
JSONArray jsonArray = new JSONArray(test.toJson(dataSnapshot.getValue()));
int totalDownloadCounter = 0;
int downloadCounter = 0;
// Determine how much files you should download first
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject cat = jsonArray.getJSONObject(i);
JSONArray sounds = cat.getJSONArray("son");
for (int j = 0; j < sounds.length(); j++){
String soundId = sound.getString("id");
if (!listSounds.contains(soundId)) {
totalDownloadCounter++;
}
}
}
for (int i = 0; i < jsonArray.length(); i++){
JSONObject cat = jsonArray.getJSONObject(i);
String nom = cat.getString("nom");
String id = cat.getString("id");
if (!listCat.contains(id)) {
db.addCategorie(nom, id);
}
JSONArray sounds = cat.getJSONArray("son");
Log.i("cat", sounds.toString());
for (int j = 0; j < sounds.length(); j++){
JSONObject sound = sounds.getJSONObject(j);
String soundId = sound.getString("id");
if (!listSounds.contains(soundId)){
//downloadFile(sound.getString("lien"));
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://soundbox-a6dd8.appspot.com");
StorageReference islandRef = storageRef.child(sound.getString("lien")+".mp3");
File rootPath = new File(Environment.getExternalStorageDirectory(), context.getString(R.string.folder_name));
if(!rootPath.exists()) {
rootPath.mkdirs();
}
final File localFile = new File(rootPath,sound.getString("lien")+".mp3");
islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.e("firebase ",";local tem file created created " +localFile.toString());
downloadCounter++;
// updateDb(timestamp,localFile.toString(),position);
if (downloadCounter == totalDownloadCounter) {
callback.onTaskFinished();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.e("firebase ",";local tem file not created created " +exception.toString());
downloadCounter++;
if (downloadCounter == totalDownloadCounter) {
callback.onTaskFinished();
}
}
});
db.addSound(soundId, sound.getString("nom"), sound.getString("lien") ,id);
}
}
}
Log.i("sound", String.valueOf(db.getAllSound()));
Log.i("Firebase", jsonArray.toString());
Log.i("Firebase", String.valueOf(db.getAllCategories()));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Log.i("DataSyncFB", "onPostExecute");
}