Presentation is loading. Please wait.

Presentation is loading. Please wait.

DKU-MUST Mobile ICT Education Center 10. Activity and Intent.

Similar presentations


Presentation on theme: "DKU-MUST Mobile ICT Education Center 10. Activity and Intent."— Presentation transcript:

1 DKU-MUST Mobile ICT Education Center 10. Activity and Intent

2 Page  2 Goal Learn the concept of Activity. Learn the concept Intent and how to use the Intent Learn about the life cycle of the Activity.

3 Page  3 1. Activity, Intent ▶ Android 4 components  Android 4 components (1/2) Activity visual user interface focused on a single thing a user can do. Service no visual interface – they run in the background. (Regardless of the visible screen ) Service creation → Service start → service finish(close, end) Broadcast Receiver If you have broadcast message(Text messages arrive, Battery discharge, Removable SD Card, Changes in the network environment, etc…) response Content providers allow data exchange between applications

4 Page  4 1. Activity, Intent ▶ Android 4 components  Activity An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows. An application usually consists of multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time. Each activity can then start another activity in order to perform different actions. Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, it is pushed onto the back stack and takes user focus. The back stack abides to the basic "last in, first out" stack mechanism, so, when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes. (The back stack is discussed more in the Tasks and Back Stack document.) When an activity is stopped because a new activity starts, it is notified of this change in state through the activity's lifecycle callback methods. There are several callback methods that an activity might receive, due to a change in its state—whether the system is creating it, stopping it, resuming it, or destroying it—and each callback provides you the opportunity to perform specific work that's appropriate to that state change. For instance, when stopped, your activity should release any large objects, such as network or database connections. When the activity resumes, you can reacquire the necessary resources and resume actions that were interrupted. These state transitions are all part of the activity lifecycle.

5 Page  5 1. Activity, Intent ▶ Android 4 components  Service A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background. A service can essentially take two forms: Started A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself. Bound A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed. Although this documentation generally discusses these two types of services separately, your service can work both ways—it can be started (to run indefinitely) and also allow binding. It's simply a matter of whether you implement a couple callback methods: onStartCommand() to allow components to start it and onBind() to allow binding. Regardless of whether your application is started, bound, or both, any application component can use the service (even from a separate application), in the same way that any component can use an activity—by starting it with an Intent. However, you can declare the service as private, in the manifest file, and block access from other applications. This is discussed more in the section about Declaring the service in the manifest.

6 Page  6 1. Activity, Intent ▶ Android 4 components  Broadcast Receiver Base class for code that will receive intents sent by sendBroadcast(). If you don't need to send broadcasts across applications, consider using this class with LocalBroadcastManager instead of the more general facilities described below. This will give you a much more efficient implementation (no cross-process communication needed) and allow you to avoid thinking about any security issues related to other applications being able to receive or send your broadcasts. You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the tag in your AndroidManifest.xml. Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack. There are two major classes of broadcasts that can be received: Normal broadcasts (sent with Context.sendBroadcast) are completely asynchronous. All receivers of the broadcast are run in an undefined order, often at the same time. This is more efficient, but means that receivers cannot use the result or abort APIs included here. Ordered broadcasts (sent with Context.sendOrderedBroadcast) are delivered to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order. Even in the case of normal broadcasts, the system may in some situations revert to delivering the broadcast one receiver at a time. In particular, for receivers that may require the creation of a process, only one will be run at a time to avoid overloading the system with new processes. In this situation, however, the non-ordered semantics hold: these receivers still cannot return results or abort their broadcast.

7 Page  7 1. Activity, Intent ▶ Android 4 components  Content Providers Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process. When you want to access data in a content provider, you use the ContentResolver object in your application's Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results. You don't need to develop your own provider if you don't intend to share your data with other applications. However, you do need your own provider to provide custom search suggestions in your own application. You also need your own provider if you want to copy and paste complex data or files from your application to other applications. Android itself includes content providers that manage data such as audio, video, images, and personal contact information. You can see some of them listed in the reference documentation for the android.provider package. With some restrictions, these providers are accessible to any Android application.

8 Page  8 1. Activity, Intent ▶ Activity Overview  Add a Activity Generates one activity per screen In general, for each activity, create an XML file using one TestActivity.java Code TestActivity.java is activity. Because TestActivity inherits Activity class.

9 Page  9  Activity Test When you click button in the main activity(main screen), show the second activity(second screen)  Project Info. Project Name : Project10_1 Package Name : com.cookandroid.project10_1 build SDK : Goolge API 15 or 16 Minimum Required SDK : API 15 or 16 Activity Name : Project10_1Activity Layout Name : main Title : Project10_1 [Practice 10-1] Add a new activity(1/6) 1. Activity, Intent ▶ Activity

10 Page  10  Design the Screen Make the 2 file ( main.xml, second.xml ) 1. Activity, Intent ▶ Activity [Practice 10-1] Add a new activity(2/6)

11 Page  11  Java Coding 1 Add a new activity(SecondActivity.java) file Autocomplete the onCreate( ) method. - override 1. Activity, Intent ▶ Activity [Practice 10-1] Add a new activity(3/6)

12 Page  12  Java coding 2 1. Activity, Intent ▶ Activity [Practice 10-1] Add a new activity(4/6)

13 Page  13  Java coding 3 1. Activity, Intent ▶ Activity [Practice 10-1] Add a new activity(5/6)

14 Page  14  Project execution and error messages Activity can be used as additional means must be registered in AndroidManifest.xml. 1. Activity, Intent ▶ Activity [Practice 10-1] Add a new activity(6/6)

15 Page  15  Explicit Intent and Data Transfer Explicit Intents have specified a component (via setComponent(ComponentName) or setClass(Context, Class)), which provides the exact class to be run. Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application. In many cases, the data type can be inferred from the URI — particularly content: URIs, which indicate that the data is located on the device and controlled by a content provider (see the separate discussion on content providers). But the type can also be explicitly set in the Intent object. The setData() method specifies data only as a URI, setType() specifies it only as a MIME type, and setDataAndType() specifies it as both a URI and a MIME type. The URI is read by getData() and the type by getType(). 1. Activity, Intent ▶ Explicit Intent

16 Page  16  RatingBar (1/2) 1. Activity, Intent ▶ Explicit Intent

17 Page  17  RatingBar(2/2) 1. Activity, Intent ▶ Explicit Intent

18 Page  18  Implicit Intent (1/2) Implicit intents do not name a target (the field for the component name is blank). Implicit intents are often used to activate components in other applications. 2. Activity, Intent Application ▶ Implicit Intent

19 Page  19  Implicit Intent (2/2) ACTION_DIAL tel:123 - Display the phone dialer with the given number filled in. Add a permission in the AndroidMahnifest.xml 2. Activity, Intent Application ▶ Implicit Intent

20 Page  20  Example of Implicit Intent : XML code 2. Activity, Intent Application ▶ Implicit Intent

21 Page  21  Example of Implicit Intent : Java code (1/3) 2. Activity, Intent Application ▶ Implicit Intent

22 Page  22  Example of Implicit Intent : Java code (2/3) 2. Activity, Intent Application ▶ Implicit Intent

23 Page  23  Example of Implicit Intent : Java code(3/3) 2. Activity, Intent Application ▶ Implicit Intent

24 Page  24  Activity Lifecycle 2. Activity, Intent Application ▶ Activity Life Cycle

25 Page  25  Activity Lifecycle Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits. An activity has essentially four states: If an activity in the foreground of the screen (at the top of the stack), it is active or running. If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations. If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere. If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state. The following diagram shows the important state paths of an Activity. The square rectangles represent callback methods you can implement to perform operations when the Activity moves between states. 2. Activity, Intent Application ▶ Activity Life Cycle

26 DKU-MUST Mobile ICT Education Center


Download ppt "DKU-MUST Mobile ICT Education Center 10. Activity and Intent."

Similar presentations


Ads by Google