RATCHET

RATCHET Labs

05/26/2011

Android: AsyncTask, UI Threading made easy

AsynkTask is a class that allows for easy use of UI Threading. AsyncTask allows you to preform background tasks in a separate thread with out disrupting the UI thread. In ShotShakr 2.0 I am using AsyncTask to filter the list of shots when a filter is selected. In order to use AsyncTask it must be subclassed and override at least one method (doInBackground(Params… )).

Here is ShotShakr’s UpdateShot Class. Here i am passing in the list of filters and shots.

private class UpdateShots extends AsyncTask<String, Void, ArrayList<Shot>> {

	    	private ArrayList<String> aFilters;
	    	private ArrayList<Shot> aShots;
	   	
	    	public UpdateShots(ArrayList<String> filters, ArrayList<Shot> allShots){
	    	  	aFilters = filters;
	    		aShots = allShots;
	    	}
	   	
	   		
	    	
	    }

doinbackground method does the work of the filtering and after completion postExecute gets fired. Here is where i update the UI Thread with the results.

	@Override
	    	protected void onPostExecute(ArrayList<Shot> result) {
	    		//Update UI Thread
	    		shotpicker.this.filteredShots = result;
	    	}


	    	@Override
	    	protected ArrayList<Shot> doInBackground(String... params) {
	    		ArrayList<Shot> newShots = new ArrayList<Shot>();
	   		
	    			//filter shots
                                // add to new list
	   		
	   		//sends to postexecute
	    		return newShots;
	    	}

Here is how to fire of the AsyncTask

new UpdateShots(filters,allShots).execute();

See a more detailed how to http://developer.android.com/reference/android/os/AsyncTask.html

Cheers

  • Facebook
  • Twitter
  • Digg
  • Print
  • email

04/06/2011

How to create a ShakeListener for Android

Here is the implementation of my ShakeListener that I used for ShotShakr. I was very lucky to find this piece of code that does all the heavy lifting for me.

public class ShakeListener implements SensorListener 
{
  private static final int FORCE_THRESHOLD = 350;
  private static final int TIME_THRESHOLD = 100;
  private static final int SHAKE_TIMEOUT = 500;
  private static final int SHAKE_DURATION = 1000;
  private static final int SHAKE_COUNT = 3;

  private SensorManager mSensorMgr;
  private float mLastX=-1.0f, mLastY=-1.0f, mLastZ=-1.0f;
  private long mLastTime;
  private OnShakeListener mShakeListener;
  private Context mContext;
  private int mShakeCount = 0;
  private long mLastShake;
  private long mLastForce;

  public interface OnShakeListener
  {
    public void onShake();
  }

  public ShakeListener(Context context) 
  { 
    mContext = context;
    resume();
  }

  public void setOnShakeListener(OnShakeListener listener)
  {
    mShakeListener = listener;
  }

  public void resume() {
    mSensorMgr = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
    if (mSensorMgr == null) {
      throw new UnsupportedOperationException("Sensors not supported");
    }
    boolean supported = mSensorMgr.registerListener(this, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_GAME);
    if (!supported) {
      mSensorMgr.unregisterListener(this, SensorManager.SENSOR_ACCELEROMETER);
      throw new UnsupportedOperationException("Accelerometer not supported");
    }
  }

  public void pause() {
    if (mSensorMgr != null) {
      mSensorMgr.unregisterListener(this, SensorManager.SENSOR_ACCELEROMETER);
      mSensorMgr = null;
    }
  }

  public void onAccuracyChanged(int sensor, int accuracy) { }

  public void onSensorChanged(int sensor, float[] values) 
  {
    if (sensor != SensorManager.SENSOR_ACCELEROMETER) return;
    long now = System.currentTimeMillis();

    if ((now - mLastForce) &gt; SHAKE_TIMEOUT) {
      mShakeCount = 0;
    }

    if ((now - mLastTime) &gt; TIME_THRESHOLD) {
      long diff = now - mLastTime;
      float speed = Math.abs(values[SensorManager.DATA_X] + values[SensorManager.DATA_Y] + values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000;
      if (speed &gt; FORCE_THRESHOLD) {
        if ((++mShakeCount &gt;= SHAKE_COUNT) &amp;&amp; (now - mLastShake &gt; SHAKE_DURATION)) {
          mLastShake = now;
          mShakeCount = 0;
          if (mShakeListener != null) { 
            mShakeListener.onShake(); 
          }
        }
        mLastForce = now;
      }
      mLastTime = now;
      mLastX = values[SensorManager.DATA_X];
      mLastY = values[SensorManager.DATA_Y];
      mLastZ = values[SensorManager.DATA_Z];
    }
  }

In my implementation of this code i bumped up the FORCE_THRESHOLD to 1050. This seemed like a good threshold for as it was not to sensitive where you would loose any shot currently on display if you accidentally bumped your arm.

Implementing your ShakerListener on your activity

ShakeListener mShake = new ShakeListener(this);
mShake.setOnShakeListener(new ShakeListener.OnShakeListener() {

            public void onShake() {
                 // DO WORK
            }
});

It is really that simple. I have had a lot of luck with this implementation and hope you will to.

credit to Matthew Wiggins for the code.

  • Facebook
  • Twitter
  • Digg
  • Print
  • email

10/27/2010

ShotShakr Android Application

Released in May by Matt Krebs and Robb Akerson. The ShotShakr app surpassed  over 15,000 downloads  in the Android Market.  The  app suggest shots, provides recipes and allows the user to socialize the experience via Twitter and Facebook.

Check it out http://www.appbrain.com/app/com.ratchet.ShotShakr 

  • Facebook
  • Twitter
  • Digg
  • Print
  • email