02_Android_Basics
02_Android_Basics
• You can savely skip this lecture on Android basics if you know
− How Android packages (APKs) are composed
− What Intents are and how they are sent between apps
− What Activities, BroadcastReceiver, Service, and ContentProvider
components are
− What the package visibility of Android is
4 https://developer.android.com/guide/platform
Big Picture
5 https://developer.android.com/guide/platform
Applications
6
Application packages (APK)
7
Application packages (APK): Basic structure
• Applications can contain native code (C/C++ shared libraries) and resources APK
(e.g., images)
− Native code provided as shared library files that can be dynamically linked into
the process
− Resources and assets: String values, layout definitions, drawables (pictures),
raw data
• META-INF/ contains the application certificate and package manifest
− Package manifest not to be mistaken with the application manifest !
8
Application packages (APK)
9
Application Manifest.xml
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name” App info (Launcher icon, name, etc.)
...
android:roundIcon="@mipmap/ic_launcher_round"
tools:targetApi="31">
<activity android:name=".MainActivity”
android:exported="true">
Activity component
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=”.MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
BroadcastReceiver component
</intent-filter>
</receiver>
...
</application>
</manifest> https://developer.android.com/guide/topics/manifest/manifest-intro
10
Gradle build configuration
android {
namespace = ”com.example.app"
compileSdk = 33
defaultConfig {
applicationId = ”com.example.app"
minSdk = 33
targetSdk = 33
versionCode = 1
versionName = "1.0"
...
}
...
11
Components and
Intents
Activity component
• Activity lifecycle:
https://developer.android.com/guide/components/activities/activity-lifecycle
13
Intent messages
https://developer.android.com/guide/components/intents-filters
14
Application Manifest.xml: Exported attribute
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name” App info (Launcher icon, name, etc.)
...
android:roundIcon="@mipmap/ic_launcher_round"
tools:targetApi="31">
<activity android:name=".MainActivity”
android:exported="true">
Activity component
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Activity (explicitly) <category android:name="android.intent.category.LAUNCHER" />
and receiver </intent-filter>
</activity>
(implicitly) are <receiver android:name=”.MyReceiver" >
<intent-filter>
exported! <action android:name="android.intent.action.BOOT_COMPLETED" />
BroadcastReceiver component
</intent-filter>
</receiver>
...
</application>
</manifest> https://developer.android.com/guide/topics/manifest/manifest-intro
15
Intent messages
− Implicit: Set an Action string, Category, and Data; the Android framework will
find a suitable receiver for this Intent
− Action = Intent.ACTION_VIEW ; Data=“http://www.google.com”
will open an app that can show the website, e.g., the default browser app
Intent(String action)
Create an intent with a given action.
16
Example Intents
Intent intent = new Intent(Intent.ACTION_VIEW); Will implicitly start an application that can handle
intent.setData(Uri.parse("http://www.google.com")); http URIs, e.g., a browser, which then will react to
activity.startActivity(intent); this data, here, e.g., showing the Google website
https://developer.android.com/guide/components/intents-filters
17
Intent Filters
<activity android:name="ShareActivity">
<!-- This activity handles "SEND" actions with text data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
18
User-choice of applications
19
Service components
20
Service lifecycle
https://developer.android.com/guide/components/services
21
Service lifecycle
https://developer.android.com/guide/components/services
22
Starting an Unbounded Service
23
Service lifecycle
https://developer.android.com/guide/components/services
24
Stubs and Proxies: Abstract view
App A App B
Component
(e.g. Activity)
B Service Stub
Call foo(“bar”)
Stub implementation
int foo(String) { return 42; }
B Service Proxy
int foo(String);
Kernel
25
Example Service Interface Definition in AIDL
MyService.aidl:
package com.example.android;
interface MyService {
int foo(in String bar);
}
26
Example Auto-generated Proxy and Stub from AIDL
IMyService.java:
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /Users/sven/android_coding_share/apps/TestApp/src/com/example/android/MyService.aidl
*/
package com.example.android;
public interface MyService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.example.android.MyService
{
private static final java.lang.String DESCRIPTOR = "com.example.android.MyService";
...
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
...
case TRANSACTION_bar:
{
data.enforceInterface(DESCRIPTOR); Stub: Receiving side, i.e., has to be
String _arg0;
_arg0 = data.readString();
int _result = this.foo(_arg0); implemented by the Service-side (e.g., takes
reply.writeNoException();
reply.writeInt(_result);
return true;
care of reading the Parcel and calling
}
...
private static class Proxy implements com.example.roundtrip2.IRR2Service
method implementation)
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
...
@Override public int foo(String bar) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(bar);
mRemote.transact(Stub.TRANSACTION_foo, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
...
return _result; Proxy: Used by sender to call the service
}
... (takes care of writing a Parcel and sending
(“transact”) it to remote process)
27
Example Stub implementation
MyService.java:
28
Stubs and Proxies: Concrete example
• Manager classes
encapsulate the
proxy classes and 1
provide easy-to-
use APIs to invoke 2
the services 5
Source: https://thenewcircle.com/s/post/1340/Deep_Dive_Into_Binder_Presentation.htm#slide-11
29
Local Bound Service
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods.
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
content://com.example.android.BookProvider/book/23
com.example.android.BookProvider
31
Example Querying a Provider
String URL = "content://com.example.android.BookProvider/book/";
mCursor = getContentResolver().query(
URL, // The content URI of the books table
mProjection, // String[] of table columns to return for each row
mSelectionClause // Selection criteria (“where” clause)
mSelectionArgs, // Selection criteria (arguments for “where” clause)
mSortOrder); // The sort order for the returned rows
mCursor.moveToFirst();
while (cursor.moveToNext()) {
// Do something with the row
int id = mCursor.getInt(0); // id is first column
String title = mCursor.getString(1); // Title is in 2nd column
int author_id = mCursor.getInt(2); // Author ID is in 3rd column (x-ref to author
table)
}
mCursor.close();
32
BroadcastReceiver components
− Exceptions: https://developer.android.com/guide/components/broadcast-exceptions
33
Android 11: New restrictions on package visibility
https://developer.android.com/training/package-visibility
34
Android 11: New restrictions on package visibility
https://developer.android.com/training/package-visibility/declaring
35
Recap
36