Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Wednesday, January 18, 2012

Android Coding - AsyncTask Simple Example

Coming from a C++ background I have found it very insightful to code in Java with Android. I have found that Java lends itself to Object Orientation more readily than C++.  In this case inheriting from Android's AsyncTask Class.

AsyncTask is a way to easily perform asynchronous tasks.  Extremely useful when updating the UI.  In fact, you cannot simply write a For Loop in your base Android code and have it update the UI per each iteration.  It will only update the UI at the end.

My example is simple: take an imageView that has been defined in the main.xml file and update the image that it contains every 500ms after pressing a button.

For this example I assume you have already created the basic structure of your program with an imageView, a button, your images and the structure to allow you to click the button and perform an action.


Call the AsyncTask
This code should go where you are calling your button
// Define the # of iterations to change the image
Integer runLength = new Integer(4);
      
// Link to your imageView in your GUI
imageview1 = (ImageView)findViewById(R.id.imageView1);
      
// Call the AsyncTask
new ChangeGuiImages2().execute(runLength); 
Execute the AsyncTask
This code should go in the same function as the rest of your code (for my example it was directly after the onCreate function)
private class ChangeGuiImages2 extends 
   AsyncTask <integer, boolean integer> {

   // This function performs the task (does not update the UI)
   @Override
   protected Boolean doInBackground(Integer... runLength){
      
      // Make a copy of the runLength Integer
      int l_runLength = runLength[0].intValue();
      
      
      // Run you loop to update the imageView
      for (int i=0; i < l_runLength; i++){
       
         // Log what the function is doing
         Log.d(TAG,"doInBackround"+i);
       
         // Call the progress function, pass the current loop iteration
         Integer progress = new Integer(i);
         publishProgress(progress);
          
         // Sleep for 500ms
         SystemClock.sleep(500);
      }
   return null;    
   }
     
   // This function is used to send progress back 
   //   to the UI during doInBackground
   @Override
   protected void onProgressUpdate(Integer...integers){
      
      // Log what the functions is doing
      Log.d(TAG,"onProgressUpdate:"+integers[0]);
      
      // Create a local copy of the integer 
      int l_integers = integers[0].intValue();
      
      // Set the imageview to the appropriate image based on iteration. 
      //   (sample_0, sample_1, ... etc.)
      imageview1.setImageResource(getResources().getIdentifier(
          "sample_"+l_integers, "drawable", getPackageName()));
   }
     
   // This function is called when doInBackground is done
   @Override
   protected void onPostExecute(Boolean b){
      
      // Log what the functions is doing
      Log.d(TAG,"onPostExecute");
      
   }     
} 

Monday, February 22, 2010

C++ Code for Minimum Value

Here is some code that I wrote while learning about vectors and templates.  The code takes a large sequence of numbers and finds the smallest value for a smaller segment of the larger sequence.  This is repeated for each small segment that fits into the larger segment.

Example:
   We have a large sequence (size 20) and want to find the minimum value for each small segment (size 5).

Large Sequence:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

1st Small Sequence               Output
 1 2 3 4 5                                   1

2nd Small Sequence              Output
 6 7 8 9 10                                 6

3rd Small Sequence               Output
 11 12 13 14 15                         11

4th Small Sequence               Output
 16 17 18 19 20                         16
 Code:
    Here is the code

Thursday, September 24, 2009

Some Interesting Bash Functions

In Linux there is a type of scripting that can be done in the Bash shell.  This is a very powerful feature of Linux, as Bash offers many advanced features.  This blog will show some of the features I have learned.  I will not attempt to cover a tutorial but rather varying functions that I have found useful.

IFS
   One of the interesting things that I have learned is the usage of IFS.  According to wikipedia it stands for Internal Field Separator.  What it does is allow you to define what Bash defines as a separator.  Bash defaults to space as the separator, therefore functions that utilize IFS will separate items based on spaces.

  While scripting you might want to change this.  Maybe a comma or a new line will be what is wanted.  Here is an example that sets the value to a new line and then back to the default.


#set IFS to be a new line
IFS="
"
#set IFS to be a space
IFS=" " 

Checking Arguments
  When you are implementing a bash script you can pass in arguments to the program.  Each argument is listed as $(value) where value ranges from 0 on up.  $0 is your program, $1 is the first passed in argument, etc.

Now what we want to do is check how many the user input.  Maybe we only want 2 values to be input, so we should check that.  Here is an example that checks for 2 inputs.


if [ $# -ne 2 ]; then
  echo "You did not input 2 arguments, try again!"
  exit
fi

Iterate Through all Files in CWD
  Sometimes it is necessary to iterate through all files in a certain directory.  There are a few ways to do this, but if you are dealing with files that may have spaces in them, the best way is the following.


for file in `ls -A $src`; do 
  sourceFile="$src/$file"
  # Now do what you want it to do with each source file
done


* Note: In the above example you will note $src, this is the full length path where you are searching files.  This can be a hard coded variable or possibly an argument to your function.

* Note: Whenever you reference sourceFile make sure to use quotes

Checking for Certain Things
  When you are coding you might want to check what something is.  Here are a few ways to check.

Checking that a directory exists

if [ ! -d "$dir" ]; then
  #do something since the directory does not exist
fi

Checking for a regular file

if [ -f "$file" ]; then
  #do something since it is a normal file
fi

Checking if file1 is newer than file2

if [ "$file1" -nt "$file2" ]; then
  # file1 is newer, so do something here
fi

Wednesday, August 26, 2009

How to post code on your blog

I was unaware of how to do this and did some searching online. There are many ways of doing it. The following blog has a great way to do it and the directions are very clear and to the point.

Vivian's Tech Blog - How to post source code in blogspot.com