Thursday, August 22, 2013

Canceling an AsyncTask using the back button

After the last post about AsyncTask, there is a pending task to complete the example. A task running in background must be canceled if the user demand it. Thus, this is a good practice promoted by Google. Applications must be responsives. As well as canceling a task, we are going to learn how to capture the back button. AsyncTask will be canceled when back button is pressed by the user.

For capturing back button since Android 2.0 (Api level 5), Google has implemented a new method inActivity class which must be overridden. This method is onBackPressed(). In earlier versions, back button is captured with method onKeyDown() and is identified checking if key received as parameter is KeyEvent.KEYCODE_BACK.

So, in ProgressBarExampleActivity class from the last post we need two changes:
  • Add onBackPressed() method.
  • Set ProgressBarAsyncTask class as member of the class to be cancelled fromonBackPressed() method.
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
 
public class ProgressBarExampleActivity extends Activity {
  
   private static final String LOG_TAG = "PB_EXAMPLE";
   private EditText etNumSecondsM;
   private EditText etSecondsProgressedM;
  
   private Button bExitM;
   private Button bExecuteM;
   private ProgressBar pbDefaultM;
   private ProgressBarAsyncTask pbTaskM = null;
  
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      drawGUI();
   }
 
   /** Called when user press the back button */
   @Override
   public void onBackPressed()
   {
      Log.d(LOG_TAG, "Cancelling task");
 
      if( pbTaskM != null)
      {
         pbTaskM.cancel( false);
      }
   }
  
   public void drawGUI()
   {
      Log.d(LOG_TAG, "Creating Graphic Interface");
      setContentView(R.layout.main);
         
      //Text Editors
      etNumSecondsM = (EditText) findViewById(R.id.etNumSeconds);
      etSecondsProgressedM = (EditText) findViewById( R.id.etSecondProgressed);
         
      //Buttons
      bExecuteM = (Button) findViewById(R.id.bExecute);
      bExitM = (Button) findViewById(R.id.bExit);
         
      //Progress Bar
      pbDefaultM = (ProgressBar) findViewById( R.id.pbDefault);   
         
      // When the execution button is pressed, the AsyncTask is created and
      // executed.
      bExecuteM.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
               pbTaskM = new ProgressBarAsyncTask( pbDefaultM, etSecondsProgressedM);
               pbTaskM.execute( new Integer( etNumSecondsM.getText().toString()));
            }
      });
 
      bExitM.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             exit( RESULT_OK);
         }
      }); 
    }
     
    public void exit( int theResult)
    {
       setResult( theResult);
       finish();
    }
}
 
 
 
 

Update rolling out to new Nexus 7, claims to fix multitouch issues

When the second generation Nexus 7 was launched, it was plagued with many problems. Factory images weren’t being released, people complained about multitouch issues and the GPS cut out for many. Google seems to have worked hard to make the device better, and the first two issues have been fixed. Previously, Google posted the factory images for the device on their site.
Today, they’re beginning to roll out a new update for the tablet. The update is Android build JSS15Q and is made specifically to fix the multitouch issues many people experienced. And though not everyone has received it, there are already reports that it does indeed fix the issue!
People had many theories about why these issues were present, but it was probably something as simple as a bad undervolt on the touchscreen controller. That’s why it worked better plugged in; it received the proper voltage. We’re glad to see the update fixing people’s issues without the need for an RMA. Have you received this update? Did it fix your issues?

Google Play Services 3.2

We've just finished rolling out the latest release of Google Play services to devices around the world. It offers better performance and greater power savings, as well as enhancements to the Location Based Services, maps, InstantBuy, Google+, and Photo Sphere.
To simplify your testing, we've also released an updated Google APIs emulator image that includes Google Play Services 3.2. You can download the image through the Android SDK Manager.

Maps and Location Based Services

Google Play Services 3.2 includes several enhancements to the Location Based Services. The Fused Location Provider now supports the selection of a low-power mode option when requesting location updates, and the ability to inject mock locations — allowing you to more efficiently test your apps in a variety of simulated conditions.
The geofencing APIs have been updated to support hardware-based GPS geofencing on devices that have supporting hardware, such as the Nexus 4. Hardware geofences consume significantly less battery, and best of all your app will automatically take advantage of this feature on supported hardware without you having to make any changes.
A new Snapshot feature in the maps API lets you capture a bitmap image of the current map in order to improve performance when an interactive map isn't necessary. We’ve also added a listener to the My Location button.

Google+, Photo Sphere, InstantBuy, and Analytics

If you’ve used Google+ sign-in you can take advantage of the new simplified sharing control that can be embedded directly within your app, simplifying the process of sharing content directly to Google+. We’ve also taken the opportunity to add some butter to the Google+ sign-in animation.
The Photo Sphere viewer has also been extended to include a compass mode that allows users to explore Photo Spheres by moving their phones.
The InstantBuy implementation has been improved to increase efficiency, with improved latency, a cleaner UI with contextual text and assets for the holo light theme, and support for passing through loyalty and offers information.

More About Google Play Services

To learn more about Google Play services and the APIs available to you through it, visit the Google Services area of the Android Developers site.