Android App Development Essentials

What’s Inside an APK File?

Android SDK tools compile your Java source files into .dex files and then zip them together with project resources (images, layouts, etc.) and the manifest file into an APK. You need to build the .apk from Android Studio at least once to see the apk folder on your laptop. Unzip any Android .apk, and you’ll see these files inside:

  • YourProject/app/build/outputs/apk
  • Resource: layouts, icons, images, animations, strings
  • Manifest: application-wide configuration (components, permissions)
  • dex: similar to ‘class’ files

UI files belong to ‘res’. We need the ID of a UI element when we reference/manipulate the UI component in our code.

Android Components

An activity represents a single screen with a UI.

Service

Runs in the background to perform long-running operations or does some work for remote processes.

  • No user interface.
  • Yes, we can create our own services.

Content Provider

Manages a shared set of app data.

  • Other apps can query or even modify the data.
  • You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your app can access.

Broadcast Receiver

A component that responds to system-wide broadcast announcements. Broadcast receivers don’t display a user interface; they may create a status bar notification. Examples include the battery being low, a picture being captured, or a download completing.

AndroidManifest.xml

  • <activity> elements for activities
  • <service> elements for services
  • <provider> elements for content providers
  • <receiver> elements for broadcast receivers

Activities, services, and content providers included in your source but not declared in the manifest are not visible to the system and, consequently, can never run. Broadcast receivers can be either declared in the manifest or created dynamically in code (as BroadcastReceiver objects) and registered with the system by calling registerReceiver().

Intents

An “Intent” activates another component. Three components (activity, service, and broadcast receiver) are activated by an asynchronous message called an intent. Intents bind individual components to each other at runtime.

You can start an activity (or give it something new to do) by passing an Intent to startActivity() or startActivityForResult() (when you want the activity to return a result).

  • You can start a service (or give new instructions to an ongoing service) by passing an Intent to startService(). Or you can bind to the service by passing an Intent to bindService().
  • You can initiate a broadcast by passing an Intent to methods like sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().
  • You can perform a query to a content provider by calling query() on a ContentResolver.

Explicit Intent

Explicit – using the component’s class name.

Intent x = new Intent(this, Activity2.class); startActivity(x);

Also can be used for when we want to switch to an Activity and pass some data to it:

Intent x = new Intent(this, Activity2.class); x.putExtra("msg", "some string"); startActivity(x);

To access the data in the other Activity: getIntent().getStringExtra("msg")

Implicit Intent

  • Provide action to be performed.
  • Provide data to be used.
  • Multiple matching activities may exist.

How to Receive an Intent?

You specify <intent-filter> in your manifest.

<activity android:name="MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

Types of Animation

  1. Drawable Animation
  2. View animation
  3. Property Animation

Canvas

  • Canvas holds all of your draw*() calls.
  • Drawing is performed upon an underlying Bitmap.

Two ways to use the Canvas of a View:

  • Custom View
  • Surface View

Sensors

  1. Motion Sensors

    • Measure acceleration and rotational forces.
    • e.g., accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.
  2. Position Sensors

    • Measure the physical position of a device
    • e.g., orientation sensors and magnetometers
  3. Environmental Sensors

    • Measure various environmental parameters
    • e.g., barometers, photometers, and thermometers.

Android Sensor Framework

  1. Check Availability: What sensors do I have?
  2. Get Information: e.g., maximum range, manufacturer, power requirements, and resolution.
  3. Get Values: e.g., raw sensor data, minimum rate.
  4. Register/unregister for Sensor Events

Four Relevant Classes

  1. SensorManager:
    • Create an instance of the sensor service.
    • Accessing and listing sensors.
    • Registering and unregistering sensor event listeners.
    • Report sensor accuracy, set data acquisition rates, and calibrate sensors.
  2. Sensor:
    • Create an instance of a specific sensor.
    • Determine a sensor’s capabilities.
  3. SensorEvent:
    • Carries information about a sensor event:
    • Raw data, sensor type, accuracy, timestamp
  4. SensorEventListener:
    • Two callback methods that receive notifications:
    • onSensorChanged() and onAccuracyChanged()

Cursor

  • Points to a row of the result of a query (in your Android/Java code).
  • To iterate over the rows: cursor.moveToFirst(), cursor.moveToNext()
  • To get the value of a specific column: getString(column_index), getInt(column_index)

A program is a set of instructions + data stored in an executable image (passive). A process is a program “in action” (dynamic).

  • Program counter
  • CPU registers
  • Stacks
  • States

Thread

  • A Thread is a flow of execution inside a process.
  • Threads in a process share the same virtual memory address space.
  • Context switching between threads is faster than context switching between processes.
  • Executing a long-running operation inside the click event freezes the UI.
  • Starting a new thread to run the operation solves the responsiveness problem, but the new thread cannot access UI elements.
  • Using runOnUiThread() or the UI element’s post() method, we can execute a piece of code on the UI thread.