Android Intent

Android Intent

Intent is the negotiator between two activities or between two applications. It gives the ability to pass messages and data between the two entities. When writing applications for mobile, Intent is very handy, since it gives access to a lot of the OS services like opening the camera, a browser, displaying notifications and so on.

Intents are instances of the android.content.Intent class. One of the most common uses for Intents is to start new activities. Intents can also be used to broadcast messages across the system. Any application can register Broadcast Receiver  to listen for, and react to, these broadcast Intents.

Android uses broadcast Intents to announce system events, like changes in internet connection status or battery charge levels. The native android applications such as the phone dialer & SMS Manager, simply register components that listen for specific broadcast Intents- such as "incoming phone call" or "SMS message received" - and react accordingly.

Explicit & Implicit Intents

Android system supports two types of Intents.

Explicit Intents

Explicit Intents explicitly names the component which should be called by the Android system, by using the Java class as identifier.

Intent i = new Intent(this, MyActivity.class);
i.putExtra("Value1", "This value one for MyActivity");
i.putExtra("Value2", "This value two MyActivity");

Explicit Intents are typically used within on application as the classes in an application are controlled by the application developer

Implicit Intents

Implicit Intents do not directly specify the Android components which should be called. They specify the action which should be performed and optionally an URI which should be used for this action.

For example the following tells the Android system to view a webpage. Typically the web browser is registered to this Intent but other component could also register themself to this event.

Intent intent = new Intent(Intent.ACTION_VIEW,
                     Uri.parse("http://www.androidaspect.com"));

If these Intents are send to the Android system it searches for all components which are registered for the specific action and the data type.

If only one component is found, Android starts this component directly. If several components are identifier by the Android system, the user will get an selection dialog and can decide which component should be used for the Intent.

How to start a new Activity using Intent?


Android Applications can contain more than one activities. When our application has more than one activity, then we have to navigate from one activity to another activity. In android, we can navigate between activities using Intent.

Suppose we are on Activity named ‘FirstActivity‘ and on certain event (like button click..) we want to start a new Activity named ‘SecondActivity’

We can create a new activity as follows.

public class SecondActivity extends Activity {

//Your member variable declaration here

// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
//Your code here
}
}

After we have created the new Activity, we have to register it in file ‘AndroidManifest.xml’. For registering we have to create an entry in ‘AndroidManifest.xml’ as -

<activity android:name=".SecondActivity" android:label="@string/app_name">
</activity>

(Note: Insert above code inside <application> tag)

Next you can start this activity on any event as follows-

Intent in = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(in);


Here, you have to create an intent with FirstActivity.this as first parameter and the second activity as second parameter. After you have created the intent, you can start the new activity by calling startActivity, on  current activity (FirstActivity), with the created intent as parameter.

Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes