User Tools

Site Tools


android_study

This is an old revision of the document!


Android Study

Background Processing

Doze

Doze

Alarms to not fire in Doze mode.

Foreground Services

Background Location

Saving Data

  • Shared Preferences
  • SQL

IME KEyboard Input

Starting from Android 1.5, the Android platform offers an Input Method Framework (IMF). https://android-developers.googleblog.com/2009/04/updating-applications-for-on-screen.html

An input method editor (IME) is a user control that enables users to enter text. To add an IME to the Android system, you create an Android application containing a class that extends InputMethodService.

Creating Input Method

Alarm Manager

Periodically eexecute

FusedLocationProvider

Uses Google Play API. Must add this to your gradle.build.

Must ask for ACCESS_BACKGROUND_LOCATION permission.

In Manifest:

<uses-permission android:name=“android.permission.ACCESS_COARSE_LOCATION”/>

private FusedLocationProviderClient fusedLocationClient;

// ..

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}

Getting the last location:

fusedLocationClient.getLastLocation()
        .addOnSuccessListener(this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                // Got last known location. In some rare situations this can be null.
                if (location != null) {
                    // Logic to handle location object
                }
            }
        });

LocationManager

ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION in manifest

Google Cloud Messaging (GCM)

Server informs the app on phone that there is new data on server. Battery efficient, the device does not have to poll.

Note: When using GCM, your app can pass messages in normal or high priority. Your server should typically use normal priority to deliver messages. Using this priority level prevents devices from being woken up if they are inactive and in a low-power Doze state. Use high priority messages only if absolutely required.

Pre-Fetching

  • Download 2 to 5 minutes of data likely to be used. Such as one full song.
  • 1 to 5Mb on a 3G network
  • Measure usage so you know what best to download.
  • Use HTTP live streaming where possible
  • Record and determine usage patterns to know what to prefetch
  • News all want to download a lot of articles and pictures and feed to app asynchronously to provide good user experience.
  • Possibly schedule downloads when device is charging.Intent.ACTION_BATTERY_CHANGED
  • Use Google Cloud Messaging (see above) to let the server notify app to fetch updates.
  • Track activity of user and pre-fetch when user is not interacting with the app, but has the app open. Intent.ACTION_STRING. This is “Context Driven Prefetching”

https://developer.android.com/training/efficient-downloads/efficient-network-access.html#PrefetchData

https://www.youtube.com/watch?time_continue=175&v=Rk1u7VVmadE&feature=emb_logo

Batch Network Requests

  • Network access is the biggest battery drain.
  • After a network request the chip waits around using power for an extra 20 to 60 seconds.
  • Piggy-back on other services firing up network. GCM Network Manager!

Use GCM Network Manager. It will allow you to batch requests and send/receive efficiently.

Batch Network Updates Intro Video

Android Performance Patterns Videos

https://www.youtube.com/playlist?list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE

  • Screen refreshed 60 =time/sec, every 16ms.
  • If a process will take 16ms, do it in a thread!

Threading methods:

  • AsyncTask
  • HandlerThread
  • ThreadPool
  • IntentService

HandlerThread

  • Long running thread.
  • Looper accessed by a Handler
  • Gets jobs from a queue
  • Should set thread priority!

ThreadPool Class

  • ThreadPoolExecutor manages thread Pools. Spins up threads and you toss work to them.
  • Only for large numbers of jobs
  • Lots of work into little buckets
  • Should set thread priority!

Each tread costs 64k minimum

AsyncTasks

  • queue up.
  • Waits for previous one to complete.
  • If canceled, onCanceled() is called. Should not create AsyncTask as an inner class. If Activity gets destroy its reference will remain in memory until the task completes!

Intent Service Class

  • Moves all intent responses to another thread for you
  • Acts like a service, inherits from Service
  • Makes your app less likely to be killed by the system
  • Not a general purpose threading solution. Really only for intents
  • Can call RunonUIThread or LocalBroadcast an intent to the Activity to update UI

LoaderManager

  • Avoids memory leaks when UI is destroyed but threads are active

Latency

  • Need to adapt to changing network quality
  • 1) Gather info about the network, 2) Make adjustments
  • Use connectivityManager and get the network connection type
  • Use the emulator and use “Emulator Throttling”
  • Use AT&Ts Network Attenuator

Minimize Asset Size

  • Make as small as possible to avoid network bloat
  • Large assets drain battery
  • Large assets costs money for the user
  • Don't use PNGs if not necessary
  • Play with JPG quality to reduce size
  • Store different sizes and qualities of images on your server
  • JSON and XML are horribly large
  • Protocol Buffers
  • Nano-proto Buffers
  • Flat Buffers

Use Network Monitor Tool in Android Studio to view asset usage issues

Tools to use

Systrace Network Monitor Tool - in Android Studio Network Attenuator from AT&T ARO Tool from AT&T

android_study.1583691500.txt.gz · Last modified: 2020/03/08 18:18 by jrseti