Wednesday, April 2, 2014

Intents

Intents are system messages that are responsible for notifying applications of severals events:

  • changes to the hardware (eg when the phone is inserted)
  • notifications of incoming data (eg when an SMS arrives) 
  • events in applications (eg when a activity is launched from the main menu). 


We can create activities that meet the intentions, we can also create intentions to launch activities or use them to trigger events to specific situations (eg the phone to notify us via a message when our location is 10 meters from the point of destination).
It is noteworthy that the system is choosing between the activities available on the phone and will answer the intention with the best activity.

Source

An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object. This type of intent is called an implicit intent because it does not specify the app component to start, but instead specifies an action and provides some data with which to perform the action.
When you call startActivity() or startActivityForResult()and pass it an implicit intent, the system resolves the intent to an app that can handle the intent and starts its corresponding Activity. If there's more than one app that can handle the intent, the system presents the user with a dialog to pick which app to use.

Example intent:
public void createAlarm(String message, int hour, int minutes) {
    Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
            .putExtra(AlarmClock.EXTRA_MESSAGE, message)
            .putExtra(AlarmClock.EXTRA_HOUR, hour)
            .putExtra(AlarmClock.EXTRA_MINUTES, minutes);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}
Note:
In order to invoke the ACTION_SET_ALARM intent, your app must have the SET_ALARM permission:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
Example intent filter:
<activity ...>
    <intent-filter>
        <action android:name="android.intent.action.SET_ALARM" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>



Intents are the "messages" (named and defined as structures that describe the abstract operation to do or something that has happened) which are activated by 3 basic components of an application: activities, services, and broadcast receivers (activities, services, and broadcast receivers). There are different mechanisms for delivery of Intents to each component type, here is a brief summary:


  • An Intent object is passed as a parameter to Context.starService () to start a service or deliver new instructions to a service already underway. It can also be passed as a parameter to Context.bindService () method to establish a connection between the component and the destination service. 
  • An Intent object is passed as a parameter to any method of issue (broadcast) as Context.sendBroadcast () Context.sendOrderedBroadcast () or Context.sendStickyBroadcast (), which deliver the Intent object to all broadcast receivers (Broadcast receiver) concerned.
These mechanisms are mutually exclusive, meaning that only one mechanism at a time will be used : the emission Intents (Broadcast Intents ) are delivered only to broadcast receivers ( BroadcastReceivers ) , never Activities or Services , the only Intents to start Activities delivered to an Activity , not to a service or broadcast receiver ( BroadcastReceivers ) ...

In the specific case of the third mechanism, BroadcastReceivers can restrict who can send Intent emissions , this is achieved by applying permissions in the AndroidManifest.xml file in the element <receiver> permission is checked after the method execution ended Context.sendBroadcast () since the system is delivering emissions to the receiver if it is not allowed to simply not delivered. Permission is also available with the Context.registerReceiver ( ) method to control who can issue to a registered receiver.
An Intent contains information of interest to the component that receives this Intent ( as an action to perform and the data you need that action) , and the Android system (such as the category of the component or instructions on how to launch an Activity) . 

Examples: 
● Explicit Intent: To start another Activity from a currently running is called startActivity (method), which receives as a parameter an Intent describing the activity to be started. example: 
[...] 
Intent intent = new Intent (this, OtraActivity.class); 
startActivity (intent); 
● Implicit Intent: Using Intent to open a web page implementation would be: 
Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse ("http://www.google.com")); 
startActivity (intent);


No comments:

Post a Comment