1/36

Android Development

Android Framework Basics

2/36

Agenda

3/36

Android Framework Components Overview

4/36

Framework Components Part I

Activity & Intent

5/36

Activity

6/36

Activity Lifecycle

center

7/36

Activity Lifecycle

8/36

Activity Class

9/36

Activity Class Example

...
public class MainActivity extends Activity {

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

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/36

Intent

12/36

Two Types of Intents

13/36

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/36

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/36

Intent Filter

16/36

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/36

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/36

Explicit Intent

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

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

19/36

Android Assignment 1

20/36

Android Security & Permissions

21/36

Android Security Architecture: Basic Principles

Book: N. Elenkov, Android Security Internals

22/36

Android Permissions

23/36

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/36

Android Permissions Declaration

25/36

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/36

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/36

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/36

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/36

Framework Components Part II

The WebView

30/36

Android WebView

Uri uri = Uri.parse("https://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
31/36

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("https://www.google.com");
32/36

Android WebView

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

Android WebView

myWebView.setWebViewClient(new WebViewClient(){});
webview.getSettings().setJavaScriptEnabled(true);
34/36

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/36

Summary

36/36

Recab Questions