Download presentation
Presentation is loading. Please wait.
1
Mobile Programming Dr. Mohsin Ali Memon
2
Intents in Android Intents are used as a message-passing mechanism that lets you declare your intention that an action be performed on a particular piece of data. One of the most common uses for Intents is to start new Activities, either explicitly (by specifying the class to load) or implicitly (by requesting an action be performed on a piece of data).
3
Intents in Android cont’d
Intents can also be used to broadcast messages. Any application can register a broadcast receiver to listen or react to these messages. You can create event-driven applications based on internal, system, or third-party application events. Android broadcast Intents examples are changes in Internet connection status or battery charge levels.
4
Intents to launch Activities
The most common use of Intents is to bind your application components. They start, stop, and transit between the Activities within an application. After calling startActivity, the new Activity (in this example, MyOtherActivity) will be created and become visible and active, moving to the top of the Activity stack.
5
Implicit Intents Implicit Intents are a mechanism that lets anonymous application components service action requests. When constructing a new implicit Intent to use with startActivity, nominate an action to perform and, optionally, supply the data on which to perform that action.
6
Implicit Intents cont’d
Android resolves this intent and starts an activity that provides dial action on a telephone number. Various native applications provide components to handle actions performed on specific data. Third party applications, including your own, can be registered to support new actions.
7
Native Android Actions
ACTION_ANSWER Opens an Activity that handles incoming calls. Currently this is handled by the native phone dialer. ACTION_CALL Brings up a phone dialer and immediately initiates a call using the number supplied in the data URI. Generally, it’s considered better form to use the Dial_Action if possible. ACTION_DIAL Brings up a dialer application with the number to dial prepopulated from the data URI. By default, this is handled by the native Android phone dialer.
8
Native Android Actions cont’d
ACTION_PICK Launches a sub-Activity that lets you pick an item from the URI data. When closed, it should return a URI to the item that was picked. The Activity launched depends on the data being picked; for example, passing content://contacts/people will invoke the native contacts list. ACTION_SEARCH Launches the UI for performing a search. Supply the search term as a string in the Intent’s extras using the SearchManager.QUERY key. ACTION_SENDTO Launches an Activity to send a message to the contact specified by the data URI.
9
Native Android Actions cont’d
ACTION_VIEW The most common generic action. View asks that the data supplied in the Intent’s URI be viewed in the most reasonable manner. Different applications will handle view requests depending on the URI schema of the data supplied. Natively, http: addresses will open in the browser, tel: addresses will open the dialer to call the number, geo: addresses are displayed in the Google Maps application, and contact content will be displayed in the Contact Manager. ACTION_WEB_SEARCH Opens an activity that performs a Web search based on the text supplied in the data URI.
10
Introducing Linkify The Android framework provides an easy way to automatically convert text patterns into clickable links. By default, Android knows how to recognize web URLs, addresses, map addresses, and phone numbers, but it also includes a flexible mechanism for recognizing and converting additional text patterns, as well.
11
TextView’s URL linking
TextView myWebSite = new TextView(………); myWebSite.setText(" Linkify.addLinks(myWebSite, Linkify.WEB_URLS); startActivity(new Intent(Intent.ACTION_VIEW, uri)) This method is implicitly fired when user clicks the link
12
Phone Number & Map linking
TextView myPhone = (TextView)findViewById(R.id.my_web_site); myPhone .setText(“ ”); Linkify.addLinks(myPhone , Linkify.PHONE_NUMBERS); TextView myLocation = new TextView(this); myLocation.setText("436 Mayfield Ave, Stanford, CA"); Linkify.addLinks(myLocation , Linkify.MAP_ADDRESSES); mainLayout.addView(myLocation);
13
Using patterns in linking
TextView myCustomLink = new TextView(this); Pattern pattern = Pattern.compile("[a-zA-Z]+&"); myCustomLink.setText("press Linkify& or on Android& to search it on google"); Linkify.addLinks(myCustomLink,pattern, " mainLayout.addView(myCustomLink);
14
Returning Results from Activities
An Activity started using startActivity is independent of its parent and will not provide any feedback when it closes. Alternatively, you can start an Activity as a sub-Activity that’s inherently connected to its parent. Sub-Activities trigger an event handler within their parent Activity when they close. They are perfect for situations in which one Activity is providing data input (such as a user selecting an item from a list) for another.
15
Returning Results from Activities cont’d
The startActivityForResult method works much like startActivity but with one important difference. You also pass in a request code with Activity to launch. This value will be used later to uniquely identify the sub-Activity that has returned a result.
16
Launching Sub-activities
This code is executed in the main activity This code is placed in the sub activity
17
Returning Results The setResult method takes two parameters: the result code and result payload represented as an Intent. The result code is the “result” of running the sub- Activity — generally either Activity.RESULT_OK or Activity.RESULT_CANCELED. This code is executed in the main activity when sub-activity finishes
18
Using Intents to Broadcast events
Within your application component, construct the Intent you want to broadcast, and use the sendBroadcast method to send it.
19
Listening for Broadcast with Broadcast Receivers
lifeformName
20
Registering Broadcast Receiver in Application Manifest
To include a Broadcast Receiver in the application manifest, add a receiver tag within the application node specifying the class name of the Broadcast Receiver to register. The receiver node needs to include an intent-filter tag that specifies the action string being listened for,
21
Native Android Broadcast Actions
Android broadcasts Intents for many of the system Services. You can use these messages to add functionality to your own projects based on system events such as time-zone changes, data-connection status, incoming SMS messages, or phone calls.
22
Native Android Broadcast Actions cont’d
ACTION_BOOT_COMPLETED Fired once when the device has completed its start-up sequence. Requires the RECEIVE_BOOT_COMPLETED permission. ACTION_CAMERA_BUTTON Fired when the Camera button is clicked. ACTION_DATE_CHANGED and ACTION_TIME_CHANGED These actions are broadcast if the date or time on the device is manually changed (as opposed to them changing through the natural progress of time). ACTION_MEDIA_BUTTON Fired when the Media button is clicked.
23
Native Android Broadcast Actions cont’d
ACTION_MEDIA_EJECT If the user chooses to eject the external storage media, this event is fired first. If your application is reading or writing to the external media storage, you should listen for this event in order to save and close any open file handles. ACTION_MEDIA_MOUNTED and ACTION_MEDIA_UNMOUNTED. These two events are broadcast whenever new external storage media are successfully added or removed from the device. ACTION_SCREEN_OFF and ACTION_SCREEN_ON Broadcasts when the screen turns off or on. ACTION_TIMEZONE_CHANGED This action is broadcast whenever the phone’s current time zone changes. The Intent includes a time-zone extra that returns the ID of the new java.util.TimeZone.
24
Dialogs in Android Dialog boxes are a common UI metaphor in desktop and web applications. They’re used to help users answer questions, make selections, confirm actions, and read warning or error messages. An Android Dialog is a floating window that partially obscures the Activity that launched it.
25
Dialogs in Android cont’d
There are three ways to implement a Dialog box in Android: Using a Dialog-Class: Android includes several specialist classes that extend Dialog. Each is designed to provide specific Dialog-box functionality. Dialog-class-based screens are constructed entirely within their calling Activity, so they don’t need to be registered in the manifest, and their life cycle is controlled entirely by the calling Activity. Dialog-Themed Activities: You can apply the Dialog theme to a regular Activity to give it the appearance of a Dialog box.
26
Example code d.show() method is called to display the dialog box.
27
Alert Dialog box The AlertDialog class is one of the most versatile Dialog implementations. It presents a message to the user offering one to three options in the form of alternative buttons. This functionality is probably familiar to you if you’ve done any desktop programming, where the buttons presented are usually a selection of OK, Cancel, Yes, or No. It also Offers a list of options in the form of check buttons or radio buttons. It Provides a text entry box for user input.
28
Alert Dialog box example
grue
29
Specialist Input Dialogs
Android includes several specialist Dialog boxes that encapsulate controls designed to facilitate common user input requests. DatePickerDialog Lets users select a date from a DatePicker View. The constructor includes a callback listener to alert your calling Activity when the date has been set.
30
Specialist Input Dialogs cont’d
TimePickerDialog Similar to the DatePickerDialog, this Dialog lets users select a time from a TimePicker View. ProgressDialog A Dialog that displays a progress bar beneath a message textbox. Perfect for keeping the user informed of the ongoing progress of a time-consuming operation.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.