1/37

Lecture Mobile Application Development (119310)

Android Framework Basics

2/37

Agenda

3/37

Android Framework Components Overview

4/37

Framework Components Part I

Activity & Intent

5/37

Activity

6/37

Activity Lifecycle

center

7/37

Activity Lifecycle

8/37

Activity Class

9/37

Activity Class Example

...
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
...
}
10/37

Application Context

// lauches a new activity
abstract void startActivity(Intent intent) 
//launches a new service
abstract ComponentName startService(Intent service) 
//Registers a BroadCast Receiver
abstract Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) 

11/37

Intent

12/37

Two Types of Intents

13/37

Implicit Intent


Intent dialIntent = new Intent();
dialIntent.setAction(Intent.ACTION_DIAL); //specifiy the action to execute
dialIntent.setData(Uri.parse("tel:0711-1234")); //provide action data
startActivity(dialIntent);

14/37

Implicit Intent

center 1. Activity A creates an Intent with an action description and passes it to startActivity() 2. The Android System searches all apps for an intent filter that matches the intent 3. When a match is found, the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.

15/37

Intent Filter

16/37

Intent Filter

<action> Declares the intent action accepted, in the name attribute. The value must be the literal string value of an action, not the class constant. <data> Declares the type of data accepted, using one or more attributes that specify various aspects of the data URI (scheme, host, port, path) and MIME type. <category> Declares the intent category accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.

17/37

Intent Filter


<activity android:name="MainActivity">
    <!-- This activity is the main entry, should appear in app launcher -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Note:To receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter

18/37

Explicit Intent



Intent intent = new Intent (getApplicationContext(), LoginActivity.class);
intent.putExtra("Name","HdM-Login");
startActivity(intent);
...

Note: An explicit intent is always delivered to its target, regardless of any intent filters the component declares.

19/37

Android Assignment: Intents

20/37

Android Security & Permissions

Activity & Intents

21/37

Android Security Architecture: Basic Principles

Book: N. Elenkov, Android Security Internals

22/37

Android Permissions

23/37

Android Permissions Declaration


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.app.myapp" >
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    ...
</manifest>

24/37

Android Permissions Declaration

25/37

Requesting Permissions at Runtime

To request a dangerous permission at runtime, some additional code is required in your Activity class:


// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
 Manifest.permission.WRITE_CALENDAR);

26/37

Requesting Permissions at Runtime

...
 // Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
	Manifest.permission.READ_CONTACTS)) {

	// Show an explanation to the user *asynchronously* -- don't block
	// this thread waiting for the user's response! After the user
	// sees the explanation, try again to request the permission.

} else {
    ... //see next slide

27/37

Requesting Permissions at Runtime

...
else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
...    

28/37

Requesting Permissions at Runtime


@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission was granted, yay! Do the
                // contacts-related task you need to do.
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
}}}

29/37

Framework Components Part II

The WebView

30/37

Android WebView


Uri uri = Uri.parse("http://www.hdm-stuttgart.de");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

31/37

Android WebView


<WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


WebView myWebView = (WebView) findViewById(R.id.webview1);
myWebView.loadUrl("http://www.hdm-stuttgart.de");

32/37

Android WebView


<uses-permission android:name="android.permission.INTERNET" />

33/37

Android WebView


myWebView.setWebViewClient(new WebViewClient(){});


webview.getSettings().setJavaScriptEnabled(true);

34/37

Android WebView


WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {
   	ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar);
 	bar.setProgress(progress);
	}
});


<ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

35/37

Android Assignment: WebView

36/37

Summary

37/37

Recab Questions