Android Open Source - 2014-Droid-code Multi Thread






From Project

Back to project page 2014-Droid-code.

License

The source code is released under:

GNU General Public License

If you think the Android project 2014-Droid-code listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cs534.sample.multithread;
/* w ww. ja va  2  s  .  c o m*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/*
 * This code is an example of doing I/O in a background thread
 * using AsyncTask.  This example is atypical as it only uses the
 * background thread by overriding doInBackground()
 * 
 * remember AsyncTask is good for fairly short running tasks
 * anything long running Java Threads are better.
 * 
 *  The I/O done here is to read a file from the Android SD card
 *  path /sdcard
 *  
 *  To run this code: 
 *  
 *  1 create a small file: 
 *  a. shell into the device  adb shell
 *  b. ls / >  /sdcard/ascii.txt
 *  c. run the app
 *  d. watch the log adb logcat -s MTHREAD
 *  note that the I/O will probably finish before you can page through any of it
 *  
 *  2 create a large file: 
 *  a. shell into the device  adb shell
 *  b. ls -lR / >  /sdcard/ascii.txt
 *  c. run the app
 *  d. watch the log adb logcat -s MTHREAD
 *  note that the I/O will probably continue in the background 
 *  as you are paging through the file 
 *  
 */
public class MultiThread extends Activity implements OnClickListener {
  private boolean aboutTVoff = true;
  private TextView tv, tv2;
  private String data = "";
  private Time today;
  private final String FN = "/sdcard/ascii.txt";
  private final String TAG = "MTHREAD";
  int start = 0;
  Button button, button2;
  GetDataAsync gdAsync = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_multi_thread);
    button = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button.setOnClickListener(this);
    button2.setOnClickListener(this);
    tv2 = (TextView) findViewById(R.id.tv2);
    tv = (TextView) findViewById(R.id.textView1);
    today = new Time(Time.getCurrentTimezone());
    today.setToNow();
  }

  /*
   * This activity implements OnClickListener so here in onClick we check to
   * see which button was pushed there are only 2. 
   * 
   * (non-Javadoc)
   * @see android.view.View.OnClickListener#onClick(android.view.View)
   */
  public void onClick(View v) {
    Log.d(TAG, "data leng "+data.length() + " start " + start);
    if (v.getId() == R.id.button1) {
      // if thread still running cancel before new thread
      // I only want 1 background thread
      if (gdAsync != null) 
        gdAsync.cancel(true);
      gdAsync = new GetDataAsync();
      gdAsync.execute(FN);
      start = 0;   // start at beginning of buffer
      if (data.isEmpty()) {
        tv.setText("finished " + today.format("%F: %T") + "\nno data");
      } else {
        String str = "finished " + today.format("%F: %T") + "\n "
            + data.substring(start, data.length() >= 100 ? 100   : data.length());
        tv.setText(str);
        button2.setVisibility(View.VISIBLE);
      }
    } else {
      // 2nd button  
      if (data.length() > (start + 100)) {
        start += 100;
        int remaining = data.length() - start;
        tv.setText("finished " + today.format("%F: %T") + "\n "
            + data.substring(start, remaining  >= 100 ? start + 100 : start + remaining));
      } else {
        tv.setText("End of Data");
      }

    }
  } // onClick()

  /*
   * Simple form of AsyncTask We do only the background task, none of the
   * callback methods that run on the UI are used.
   * 
   * The only parameter is the file name.
   * 
   * progress and result are not used hence Void
   */
  public class GetDataAsync extends AsyncTask<String, Void, Void> {
    // runs on background thread
    @Override
    protected Void doInBackground(String... fn) {
      Log.d(TAG, "Reading: " + fn[0]);
      getData(fn[0]);
      return null;
    }
    // runs on main ui thread
    @Override
    protected void onCancelled() {
      Log.d(TAG, "BG Thread Canceled");
    }
  } // AsyncTask class

  /**
   * getData() This method opens a FileInputStream, then uses a BufferedReader
   * to read the data in line by line catches file not found & loading
   * exceptions
   */
  private void getData(String fn) {
    String line = null;
    data = "";
    try {
      Log.d(TAG, "getData() " + fn);
      FileInputStream fis = new FileInputStream (new File(fn)); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
          fis));
      // check if canceled: 
      // getData is run from an AsyncTask 
      // which can be canceled by outside forces
      while (!gdAsync.isCancelled() && (line = reader.readLine()) != null) {
        data += line + "\n";
        Log.d(TAG, "line:  " + line);
      }
      reader.close();
      fis.close();
      Log.d(TAG, "File successfully loaded.");
    } catch (FileNotFoundException e) {
      Log.d(TAG, "Error file not found: " + e.getLocalizedMessage());
      data = "Error file not found: " + e.getLocalizedMessage();
    } catch (IOException e) {
      Log.d(TAG, "Error loading file: " + e.getLocalizedMessage());
      data = "Error loading file: " + e.getLocalizedMessage();
      e.printStackTrace();
    }
  } // getData()

  /*
   * User presses back key, abort the background task if running
   *  
   * (non-Javadoc)
   * @see android.app.Activity#onBackPressed()
   */
  @Override
  public void onBackPressed() {
    super.onBackPressed();
    if (gdAsync != null)
      gdAsync.cancel(true);
  }
  /*
   * If we're ending the task , abort the background task if running,
   * and not already canceled.
   *  
   * (non-Javadoc)
   * @see android.app.Activity#onStop()
   */
  @Override
  public void onStop() {
    super.onStop();
    if (gdAsync != null && !gdAsync.isCancelled()) {
      gdAsync.cancel(true);
    }
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_single_thread, menu);
    return true;
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId())
    {
    case R.id.about:
      if (aboutTVoff) {
        tv2.setVisibility(View.VISIBLE);
        aboutTVoff = false;
      } else {
        tv2.setVisibility(View.GONE);
      }
      return true;
    default:
      return false;
    }
  }
} // Activity class





Java Source Code List

ca.campbell.httpexample.HttpExample.java
ca.campbell.httpexamplepost.HttpsExamplePOST.java
ca.campbell.layoutprogrammatically.MainActivity.java
ca.campbell.networkcheckstatus.MainActivity.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity3.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.simplegridview.MainActivity.java
ca.campbell.week2_rw_views.Activity2.java
ca.campbell.week2_rw_views.MainActivity.java
com.androidbook.simpleasync.ChoiceActivity.java
com.androidbook.simpleasync.SimpleAsyncActivity.java
com.androidbook.simpleasync.SimpleNoBGThread.java
com.androidbook.simpleasync.SimpleThreadActivity.java
com.cookbook.internet.search.GoogleSearch.java
com.introtoandroid.simplefragments.FieldNoteListFragment.java
com.introtoandroid.simplefragments.FieldNoteViewActivity.java
com.introtoandroid.simplefragments.FieldNoteWebViewFragment.java
com.introtoandroid.simplefragments.SimpleFragmentsActivity.java
com.introtoandroid.simplelayout.FrameLayoutActivity.java
com.introtoandroid.simplelayout.GridLayoutActivity.java
com.introtoandroid.simplelayout.LinearLayoutActivity.java
com.introtoandroid.simplelayout.MenuActivity.java
com.introtoandroid.simplelayout.MultipleLayoutActivity.java
com.introtoandroid.simplelayout.RelativeLayoutActivity.java
com.introtoandroid.simplelayout.SimpleLayoutActivity.java
com.introtoandroid.simplelayout.TableLayoutActivity.java
com.introtoandroid.viewsamples.ButtonsActivity.java
com.introtoandroid.viewsamples.ContainersActivity.java
com.introtoandroid.viewsamples.EventsActivity.java
com.introtoandroid.viewsamples.FormsActivity.java
com.introtoandroid.viewsamples.IndicatorsActivity.java
com.introtoandroid.viewsamples.MenuActivity.java
com.introtoandroid.viewsamples.PickersActivity.java
com.introtoandroid.viewsamples.TextDisplayActivity.java
com.introtoandroid.viewsamples.TextInputActivity.java
com.introtoandroid.viewsamples.ViewSampleActivity.java
cs518.sample.activityLifecycle.Activity2.java
cs518.sample.activityLifecycle.MyActivityLifeCycleActivity.java
cs518.sample.database.AddStudent.java
cs518.sample.database.DBHelper.java
cs518.sample.database.DatabaseActivity.java
cs518.sample.database.Thirty.java
cs518.sample.dbcursoradapter.AddStudent.java
cs518.sample.dbcursoradapter.DBHelper.java
cs518.sample.dbcursoradapter.DatabaseActivity.java
cs518.sample.dbcursoradapter.Thirty.java
cs518.sample.localisation.MainActivity.java
cs518.sample.multiactivity.Activity1.java
cs518.sample.multiactivity.Activity2.java
cs518.sample.multiactivity.Activity3.java
cs518.sample.multiactivity.Activity4.java
cs518.sample.multiactivity.Activity5.java
cs518.sample.multiactivity.Activity6.java
cs518.sample.multiactivity.Constants.java
cs518.sample.usecalendarcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovidercursorloader2.MainActivity.java
cs518.sample.usemediastorecontentprovider.MainActivity.java
cs518.samples.imageswap.MainActivity.java
cs518.samples.sharedpreferences.MainActivity.java
cs534.sample.dbAsyncTask.AddStudent.java
cs534.sample.dbAsyncTask.DBHelper.java
cs534.sample.dbAsyncTask.DatabaseActivity.java
cs534.sample.dbAsyncTask.Thirty.java
cs534.sample.implicitintents.MainActivity.java
cs534.sample.multithread.MultiThread.java
cs534.sample.simplelistview.SimpleLV.java
cs534.samples.simplestlv.MainActivity.java