Efficient Android Threading: Asynchronous Processing Techniques for Android Applications

Efficient Android Threading: Asynchronous Processing Techniques for Android Applications

Anders Goransson

Language: English

Pages: 280

ISBN: 1449364136

Format: PDF / Kindle (mobi) / ePub

Multithreading is essential if you want to create an Android app with a great user experience, but how do you know which techniques can help solve your problem? This practical book describes many asynchronous mechanisms available in the Android Sdk, and provides guidelines for selecting the ones most appropriate for the app you’re building.

Author Anders Goransson demonstrates the advantages and disadvantages of each technique, with sample code and detailed explanations for using it efficiently. The first part of the book describes the building blocks of asynchronous processing, and the second part covers Android libraries and constructs for developing fast, responsive, and well-structured apps.

  • Understand multithreading basics in Java and on the Android platform
  • Learn how threads communicate within and between processes
  • Use strategies to reduce the risk of memory leaks
  • Manage the lifecycle of a basic thread
  • Run tasks sequentially in the background with HandlerThread
  • Use Java’s Executor Framework to control or cancel threads
  • Handle background task execution with AsyncTask and IntentService
  • Access content providers with AsyncQueryHandler
  • Use loaders to update the Ui with new data

PC Gamer (August 2015)

SSH, The Secure Shell: The Definitive Guide (2nd Edition)

IPv6 Essentials (2nd Edition)

Program Arcade Games: With Python and Pygame (4th Edition)

Beyond Artificial Intelligence: The Disappearing Human-Machine Divide (Topics in Intelligent Engineering and Informatics, Volume 9)



















necessary, since that may block other threads when not necessary, leading to slower execution across the application. Synchronizing on other objects’ intrinsic locks enables the use of multiple locks within a class. An application should strive to protect each independent state with a lock of its own. Hence, if a class has more than one independent state, performance is improved by using several locks. The synchronized keyword can operate in different intrinsic locks. Keep in mind that

mBackgroundThread.exit(); } // ... The rest of the Activity is defined further down } A background thread with a message queue is started when the HandlerExam pleActivity is created. It handles tasks from the UI thread. When the user clicks a button, a new task is sent to the background thread. As the tasks will be executed sequentially on the background thread, multiple button clicks may lead to queueing of tasks before they are processed. The background thread is stopped when the

implementation detail and may change. 3. See the platform-tools/aidl file in the Android SDK directory for information about the compiler. AIDL | 77 Figure 5-2. Construction of the remote communication interface The generated Java interface is included in all the client applications and in the server application. The interface file defines two inner classes, Proxy and Stub, that handle all the marshalling and unmarshalling of the data, as well as the transaction itself. Hence, the creation

with the associated Handler in the Service. Process incoming messages. On the client side, an Activity binds to the Service in the server process and sends messages: public class MessengerOnewayActivity extends Activity { private boolean mBound = false; private Messenger mRemoteService = null; private ServiceConnection mRemoteConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { mRemoteService = new Messenger(service); mBound = true; }

process and for how long. The scheduling strategy can be im‐ plemented in various ways, but it is mainly based on the thread priority: a high-priority thread gets the CPU allocation before a low-priority thread, which gives more execution time to high-priority threads. Thread priority in Java can be set between 1 (lowest) and 10 (highest), but—unless explicitly set—the normal priority is 5: myThread.setPriority(8); If, however, the scheduling is only priority based, the low-priority threads may

Download sample


About admin