Android Intents PDF - Programming Guide
Document Details

Uploaded by DeliciousNovaculite7475
Wissam HLAYHEL
Tags
Summary
This PDF document provides an overview of Android Intents, a crucial component of Android app development. It covers intent types, actions, data handling, extras, and examples. It's a useful guide for developers working with Android applications.
Full Transcript
Android Intents Wissam HLAYHEL ▪ An Intent is a messaging object used to request an action from another app component, it can be used to : ▪ Start a new Activity : new screen do specific work ▪ Starting a service : background task without GUI...
Android Intents Wissam HLAYHEL ▪ An Intent is a messaging object used to request an action from another app component, it can be used to : ▪ Start a new Activity : new screen do specific work ▪ Starting a service : background task without GUI Intent (ex: file download) ▪ Delivering a broadcast : message delivered to all apps (boot-up, device start charging) ▪ Two types of intents: What ? ▪ Explicit intents : specify the component to start by its physical name (used for inner Application component, is defined in your own app). ▪ Implicit intents : do not name a specific component, but declare a general action to perform like showing How ? a map location or opening a web site. Android system finds the appropriate component to handle it by checking intent filters declared in other’s applications manifest. ▪ If one match found, system starts that component directly ▪ If many found, system displays a list to pick which app to use. ▪ Use method (resolveActivity(getPackageManager())==null) to avoid Exception when no component match the Intent. ▪ Android uses the following Intent-fields for content resolution : ▪ Component name : optional used for “Explicit Intent”, it specifies the Activity class name to start. Intent ▪ Action : A string that specifies a generic action to perform (ACTION_VIEW, ACTION_SEND, ACTION_PICK). ▪ Data : The URI that references the data to be acted on and/or the MIME type of that data. For example, if the How ? action is ACTION_EDIT, the data should contain the URI of the document to edit. ▪ Category : additional information about the kind of component that handle the intent. Any number of categories can be placed in an intent (using addCategory methode), but most intents do not require a category. Examples of predefined categories : CATEGORY_BROWSABLE (target activity can be started by a web browser), CATEGORY_LAUNCHER, TAB, DEFAULT ▪ Intent can carry additional information (that does not affect how it is resolved): ▪ Extras : Key-value pairs that carry additional information required to accomplish the requested action. Just as some Intent actions use particular kinds of data URIs, some Actions also use particular extras. For example, intent used to send email with ACTION_SEND, have some extras like EXTRA_SUBJECT. You can add extras data with : ▪ putExtra(String name, DataType value) methods, each accepting two parameters: the key name and the value. ▪ putExtras(Bundle b), where bundle is a created object that handle all How ? the extra data. ▪ Flags : used as metadata for the intent. The flags may instruct the Android system how to launch an activity (for example, which task the activity should belong to) and how to treat it after it's launched (for example, whether it belongs in the list of recent activities). ▪ Type : Set an explicit MIME data type. This is used to create intents that only specify a type and not data, for example to indicate the type of data to return (example of MIME types : text/plain, text/html, image/jpeg, image/*). ▪ Some Standard Intent Actions : Action name description Intent ACTION_VIEW display the data to the user ACTION_EDIT provide explicit editable access to the given data ACTION_PICK pick an item from the data, returning what was selected ACTION_DIAL dial a number (shows a UI allowing the user Actions to explicitly initiate the call. ACTION_SENDTO send a message to someone specified by the data ACTION_INSERT insert an empty item into the given container ACTION_DELETE delete the given data from its container ACTION_WEB_SEARCH perform a web search ▪ Image picker : Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivity(intent); ▪ SMS sender Intent Uri uri=Uri.parse("sms:123456"); Intent intent= new Intent(Intent.ACTION_SENDTO, uri); startActivity(intent); ▪ Phone Dialer : Uri uri=Uri.parse("tel:123456"); Intent intent=new Intent(Intent.ACTION_DIAL, uri); startActivity(intent); Examples ▪ URL Browser : Uri uri=Uri.parse("http://website.com"); Intent intent=new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); ▪ Google Search : Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, "Nikon D500"); startActivity(intent); ▪ Open Settings : Intent intent = new Intent(Settings.ACTION_SETTINGS); startActivity(intent); ▪ Android apps must request permission to access sensitive user data (such as contacts and SMS) or certain system features (such as the camera and internet access). ▪ Permissions are granted by the user when the application Intent is installed (on devices running Android 5.1 and lower) or while the app is running (on devices running Android 6.0 and higher)... But no matter which Android version your app supports, you must declare all permission requests with a element in the manifest. ▪ Even if the user grants your app the permission it Permission requested you cannot always rely on having it. Users also have the option to enable and disable permissions one- by-one in system settings. You should always check for and request permissions at runtime ▪ Permission for some Activity Actions : ▪ CALL_PHONE ▪ CAMERA ▪ SEND_SMS ▪ … Intent-Filters ▪ Defining Intent-filters : ▪ How ?