package edu.oswego.vocabv4; import android.content.Context; import android.content.Intent; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class ReaderTask implements Runnable { private Context context; private ArrayList wordList; public ReaderTask(Context context) { this.context = context; } @Override public void run() { wordList = new ArrayList<>(); Scanner scanner = null; try { scanner = new Scanner(context.getResources().openRawResource(R.raw.the_words)); int i = 0; int j = 0; Random rand = new Random(); rand.setSeed(System.nanoTime()); while (scanner.hasNext()) { String word = scanner.nextLine(); wordList.add(new Word(word)); updateProgress(i++); j = rand.nextInt(1000); try { Thread.sleep(j); } catch (Exception exp) { } } } catch (Exception e) { Log.d("OD", "try-catch failed"); } finally { scanner.close(); } postExecute(); } protected void updateProgress(Integer... progress) { Intent intent = new Intent(MainActivity.INTENT_FILTER2); intent.putExtra(MainActivity.INTENT_KEY2, progress[0]); LocalBroadcastManager.getInstance(context).sendBroadcast(intent); } protected void postExecute() { Intent intent = new Intent(MainActivity.INTENT_FILTER1); intent.putParcelableArrayListExtra(MainActivity.INTENT_KEY1, wordList); LocalBroadcastManager.getInstance(context).sendBroadcast(intent); } }