mobappdev (fall 2013): activities, applications, intents
TRANSCRIPT
MobAppDev
Activities, Applications, Intents
Vladimir Kulyukin
www.vkedco.blogspot.com
Outline● Activities
– Lifecycle– Lifetimes– Essential States– Callback Methods
● Applications – Security – Sandboxes– Principle of Least Privilege
● Intents
Basic Concepts
Activity
● Activity is an application screen (aka a component)● Activity provides the user a screen with which the user
can interact● Each activity is assigned a window in which it can draw
its interface● One activity can start another activity● When an new activity starts it is pushed on the back
stack and receives user focus● The back stack is a last-in-first-out data structure
Application
● Applications are packaged executables (.apk files)● All application code, data, and resources reside in a
single archive .apk file● Applications consist of one or more components, such
as activites, services, content providers, and broadcast receivers
● The manifest file must declare all components in the application and declare all application requirements
Process & Program
● A process is an executing/running instance of a program● In OS literature, processes are also referred to as tasks● A program is an executable file stored in external memory,
e.g. hard disk drives● An executable file is a binary file that has been compiled from
source code (Java) into machine code (Dalvik JVM byte code)● A program is a passive entity until it starts to run, i.e., becomes
a process or is mounted on a running process
Activities
Activity's Lifecycle
● An actvity's lifecycle is a set of states● When the current state of an activity changes, the Android OS
notifies the activity of that change so that the appropriate callback method is called (if implemented)
● The Android developer manages the activity's lifecycle by implementing the standard callback methods that are called on the activity object when its state changes (e.g., activity is created, stopped, resumed, and destroyed)
● Callback implementation is the only leverage the developer has over activities
Activity's Lifecycle: Callback Methodspublic class MyActivity {
// The activity is created
public void onCreate(Bundle savedInstanceState) { }
// The activity is about to become visible
protected void onStart() { }
//The activity is visible (it is resumed)
protected void onResume() { }
// Another activity is taking focus (it is paused)
protected void onPause() { }
// The activity is no longer visible (it is stopped)
protected void onStop() { }
// The activity is about to be destroyed
protected void onDestroy() { }
}
Image Source: http://developer.android.com/guide/components/activities.html#Lifecycle
Activity's Lifecycle Diagram
Activity's Lifetimes
● Entire lifetime – from onCreate() to onDestroy()● Visible lifetime – from onStart() to onStop(): user sees
the activity and interacts with it● Foreground lifetime – from onResume() to onPause():
activity is in front of all other activities on screen and has user focus; code in onResume() and onPause() should be lightweight because activities frequently transition in and out of foreground
Activity's Lifetimes
Activity's Three Basic States
● Running/Resumed – this activity is in the foreground of the screen and receives user focus
● Paused – This activity is partially obscured by another activity that gets into the foreground and receives user focus
– Paused activity objects are 1) in memory, 2) attached to Window Manager, 3) killable only in extremely low memory conditions
● Stopped – This activity is completely obscured by another activity– Stopped activity objects are 1) in memory, 2) detached from
Window Manager, 3) killable when memory is needed somewhere else
Callback Methods' Details
Activity.onCreate()
● onCreate() is called when the activity is created● onCreate() is the place to create views and do data
binding● onCreate() takes a Bundle that contains the previous
state of the activity if that state was persisted● onCreate() is followed by onStart()● After this method returns, the hosting process cannot
be killed w/o executing another line of the activity's code, i.e., the hosting process is not killable
Activity.onRestart()
● onRestart() is called after the activity is stopped and right before it is started again
● onRestart() is followed by onStart()● After this method returns, the hosting process
cannot be killed w/o executing another line of the activity's code, i.e., the hosting process is not killable
Activity.onStart()● onStart() is called when the activity is visible to
the user● onStart() is followed 1) by onResume() if the
activity gets into foreground and 2) by onStop() if it becomes hidden
● After this method returns, the hosting process cannot be killed w/o executing another line of the activity's code, i.e., the hosting process is not killable
Activity.onResume()
● onResume() is called right before the user begins to interact with the activity; the activity is taking user input
● onResume() is followed by onPause()● After this method returns, the hosting process
cannot be killed w/o executing another line of the activity's code, i.e., the hosting process is not killable
Activity.onPause()● onPause() is called right before Android OS resumes a different
activity● onPause() is used to persist data, stop CPU consumables
(animations, downloads, etc.)● onPause() is followed 1) by onResume() if the activity returns back
to front or 2) by onStop() if it becomes hidden– Pre 3.0: The hosting process can be killed after this method
returns without executing another line of the activity's code
– Post 3.0: The hosting process cannot be killed after this method returns without executing another line of the activity's code
Activity.onStop()● onStop() is called when the activity is no longer visible to the
user because 1) Android OS begins to destroy the activity or 2) another activity is started or resumed and is covering this activity
● onStop() is followed 1) by onRestart() if the activity is coming back to the foreground or 2) by onDestroy() if the activity is destroyed
● After this method returns, the hosting process can be killed w/o executing another line of the activity's code, i.e., the hosting process is killable
Activity.onDestroy()
● onDestroy() is called when the activity is destroyed● onDestroy() is the final call the activity receives before
it is destroyed by the OS● onDestroy() is called 1) when the activity is finishing
after a call to Activity.finish() or 2) Android OS is destroying the activity to save space
● After this method returns, the hosting process can be killed w/o executing another line of the activity's code, i.e., the hosting process is killable
Applications
Security Sandbox
● In computer security, the term sandbox refers to a security mechanism for running program separation
● Security boxes are used to execute untested/untrusted code, e.g., untrusted programs, unsigned/unverified third-parties, untrusted users and websites
● Note: the term untrusted is relative, not absolute● Sandboxes are typically realized as a set of resources the programs
can legally access: disk, memory, network, systems file inspection, etc
● The term jail is a set of limits imposed on programs by the OS kernel
Apps on Android OS● Android OS is a multi-user Linux system● Each app is a different user● Android OS assigns each app a unique Linux user ID● The user ID is known to the OS but is not known by the app● The OS sets permissions based on the user ID● Every application, by default, runs in its own Linux process: the
OS starts this process when one of the app's components is executed
● Each process has its own Dalvik JVM: each application's running code runs isolated from other apps
Process Sharing
● Apps may share the same Linux user ID and, consequently, share each other's files and resources
● Apps with the same user ID can also request to run in the same Linux process and share the same JVM: this is done when the resources must be conserved
● Apps may request permission to access data stored on the device, e.g., user contacts, Bluetooth, SD card, SMS messages, etc
● All permissions are granted at install time
Principle of Least Privilege
● PLP principle: Each app has access only to those components required to do its work and no more
● An app cannot access parts of the system for which it is not given permission
● Permissions must be explicitly declared in the manifest file
Intents
Overview
● Intents are messages that allow Android components (e.g., activities, applications, etc) to send computational requests or notifications to other Android components via Android OS
● Intents are used for requesting information, computation, or event signaling
● All intents objects are instances of android.content.Intent
Explicit and Implicit Intents
● Intents may contain data: one activity may send data to another activity
● Android supports two types of intents: explicit or implicit
● Explicit intents must know the Java class name of the component that they want to start
● Implicit intents must know the action type they want to be done for them
Explicit Intents
● If Android OS resolves an explicit intent via its Java class identifier & the component specified by the class identifier is accessible, the component will likely be launched
● Explicit intents are typically used within one application when the developers have control over the Java class identifiers of the components
Explicit Intent Example
● Intent i = new Intent(this, ImageDisplayAct_01.class);
● If ImageDisplayAct_01.class is resolved, the corresponding component is launched
● If not, there is a run-time error
Implicit Intents
● Implicit intents must specify the type of action they should perform and possibly the type of data
● If there is a component in the Android ecosystem that is registered to perform the specified action type, it will likely be launched
● If there are several components in the Android ecosystem that are registered, Android will ask the user to select one via a selection dialog
● If no component is found, nothing happens
Implicit Intent Example
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
● If there is a component registered under MediaStore.ACTION_IMAGE_CAPTURE, it will likely be launched
● Multiple components may be registered to perform the same action
Data Transfer with Intents
● Both types of intents can bundle and transfer data to other components via Android
● An intent sender creates an intent, put a few key-value pairs into its bundle and sends it to Android
● An intent receiver receives an intent, checks it for the presence/absence of specific keys and then does some action
● Keys are always Strings
Explicit Intents: Example
● Develop an app that has two Activities: PixBrowserExpIntentsActivity_01 and ImageDisplayerAct_01
● The PixBrowserExpIntentsActivity_01 activity consumes a button click and launches the ImageDisplayerAct_01 activity via an explicit intent
● The ImageDisplayerAct_01 activity displays a random image with an ImageView object
● Source code is here
Code Fragments
Image Displayer's Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/img_view_01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/img_01" />
</LinearLayout>
Main Activity's Layout<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" >
<ImageView
android:id="@+id/img_01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/img_01" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_display_txt" />
</LinearLayout>
</LinearLayout>
AndroidManifest.xml<manifest xmlns:android="http://schemas.android.com/apk/res/android" >
<application >
<activity
android:name=".PixBrowserExpIntentsActivity_01"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ImageDisplayerAct_01" android:label="Image Displayer 01">
<intent-filter>
<action android:name="android.intent.action.ImageDisplayerAct_01" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
PixBrowserExpIntentsActivity_01.onCreate()protected Button mBtnDisplay = null;
protected Activity mThisAct = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pix_browser_exp_intents_activity_01);
mBtnDisplay = (Button) this.findViewById(R.id.btn_display);
mThisAct = this;
// Button click creates an explicit intent to request a launch of ImageDisplayerAct01
mBtnDisplay.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
// 1. Create an explicit intent
Intent i = new Intent(mThisAct, ImageDisplayerAct_01.class);
// 2. Request Android to consider it.
startActivity(i); }});
}
ImageDisplayerAct_01.onCreate()
protected ImageView mImgView = null;
protected void onCreate(Bundle savedInstanceState) {
mImgView = (ImageView) this.findViewById(R.id.img_view_01);
mImgView.setImageDrawable(null);
int img_num = new Random().nextInt(8) + 1;
switch ( img_num ) {
case 1: mImgView.setBackgroundResource(R.drawable.img_01); break;
case 2: mImgView.setBackgroundResource(R.drawable.img_02); break;
// Other cses for 3 to 8
}
Toast.makeText(this, getResources().getString(R.string.randome_img_msg) + " " + img_num,
Toast.LENGTH_SHORT).show();
}
References
● http://developer.android.com/reference/android/app/Activity.html● http://developer.android.com/guide/components/fundamentals.html● http://developer.android.com/guide/components/processes-and-threads.html● http://www.linfo.org/process.html● http://en.wikipedia.org/wiki/Sandbox_(software_development)● http://en.wikipedia.org/wiki/Sandbox_(computer_security)● http://www.youtube.com/vkedco