Table of Contents

Android Study

Background Processing

Guide to Background Processing

Doze

Doze

Alarms to not fire in Doze mode.

Foreground Services

Effective Foreground Services

Background Location

Background Location Limits

Saving Data

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

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

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

Threading methods:

HandlerThread

ThreadPool Class

Each tread costs 64k minimum

AsyncTasks

Intent Service Class

LoaderManager

Latency

Minimize Asset Size

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

Service Performance Patterns

Minimize Code

Cache

Approximation

Culling

Data Serialization

USE FlatBuffers to pass data instead of JSON!

Smaller Serialized Data

Caching UI Data

CPU Frequency Scaling

Array Maps

Beware Autoboxing

Price of Enums

Trimmed and Shared Memory

Do NOT Leak Views

Location and Battery

layouts

Object Pools

Iterators or Index?

LruCache Class

ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
int availBytes = am.getMemoryClass() * 2014 * 1024;
LruCache c = new LruCache<String, Bitmap>(availBytes/8);

LINT

Hidden Cost of Transparency

Avoid Allocations in onDraw()

Strict Mode:

Custom Views and Performance

Batching Work Until Later

Image Memory Usage

bitmap.Options.setPreferredConfig(Bitmap.Config.RGB_565)

Smaller PNG data

Scaling Bitmaps

Reusing Bitmaps

Perfomance Tuning Lifestyle

Overdraw

Understanding VSYNC

Profile GPU Rendering Tool

GPU

App Launch Time

Smaller APKs

BuildTypes{ 
  Release { 
    minifyEnabled: True
    shrinkResources: True
    proguardFiles getDefaultProguardFile('proguard-android.txt', 'proguard-tools.pro')
  }
}

Tools to use

Vector Drawable

NOTES:

New Java 5 syntax:

for (Object obj : list {
}
1)
ENUM1, ENUM2, ENUM3