Learn Android Development For Beginners

Intent

Intent is the asynchronous message which allows android components to request functionality from the other component of the Android System. It generally defines as “Intention” to do some work. The intent can be used for following operations

  • Launch an Activity
  • Start Service
  • Broadcast the message
  • Start Native Application e.g. Phone Dialer, web browser, Answer phone call etc.

Intents are not always initiated by your application. It can also be initiated by system to notify the application of specific events such as the arrival of a text message.

Start Another Activity

The most common use of the Intent is to open another activity. startActivity(Intent) method is used to open the new activity. For Creating new activity right click on the “java/ com.example.myfirstapp” in your application and click on the “new -> Activity”. Please see image below

In the create activity wizard, select the “Blank Activity” and press the next button

In the next step enter “SecondActivity” as Activity Name, “second_main” as Layout Name and “Second Activity” as Title. Press the finish button to close the dialog.

On the “Finish” button, the Android Studio automatically create the Activity class, layout and put useful information into the “AndroidManifest.xml” file. Open the “AndroidManifest.xml” file to see newly added entry for the activity.

 <activity
      android:name="com.example.myfirstapp.SecondActivity"
      android:label="@string/title_activity_second" >
 </activity>
    

Now the second activity is ready to use. You can change its layout if you want. Go to the main activity, add the new button and register the click event of the button in the MainActivity.java file (as we did in previous chapter) . In the click event write a code to open new activity. The sample code is given below.

        Button button = (Button)findViewById(R.id.btnChange);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
                    startActivity(intent);
            }
        });
    

Create the object of Intent class and in the constructor pass the context and the activity class you want to open. Activity class is also the subclass of the context. You can also pass “this” if you calling method directly from the Activity class. Pass the intent object to the startActivity(intent) method.

Use Other Available Intents in Android

As we know, we can use Intent for the multiple purposes in the Android development. Intents can use for invoking internal or external components from your application. Android also allows us to invoke set of predefine applications e.g. Browser application, phone dialer, Map etc. In the next coming examples you will see how easy it is to invoke the component.

Open Phone Dialer

To invoke the phone dialer, create the Intent object and pass the “ACTION_DIAL” as a parameter. Then pass the intent to the startActivity() method.

 public static void OpenDialer(Activity activity)
 {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        activity.startActivity(intent);
 }
    

The above code opens the phone dialer of the mobile. When startActivity method passes the intent to the Android OS, It matches the registered activity against this action and opens the matched activity for it.

ACTION_DIAL is a one to one relationship between the action and what you invoke. There is another generic action name ACTION_VIEW that you can use to invoke activity. But the question is how Android would get to know which activity you want to invoke to this action. You will get its answer soon. See the example below to understand the other way.

 public static void OpenDialerUsingURI(Activity activity)
 {
        Uri uri = Uri.parse("tel:09999999");
        Intent intent = new Intent(Intent.ACTION_VIEW,uri);
        activity.startActivity(intent);
 }
    

In above example we passed ACTION_VIEW and Uri to the intent object. In this case the Android heavily relies on the Uri scheme that is being passed with the generic action. The Uri can be defined in the AndroidManifest.xml with the definition of the Activity.

Make Phone Call

For invoking the phone call, application needs to send the appropriate action to the intent and phone number that he wants to make call.

For making call the application requires a permission to make call. You can add permission that are require to your application in AndroidMenifest.xml file. Add following line in your Manifest file

    <uses-permission android:name="android.permission.CALL_PHONE" />

By adding above line in the Manifest file, your application will acquire the permission to make call. Now write a code to make call.

 public static void Call(Activity activity)
 {
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:09999999"));
        activity.startActivity(intent);
 }
    

Create the intent and set the action “ACTION_CALL” and pass the phone number using setData() method.

Open Web Browser

Invoking the web browser is as to invoke other activity. Use action “ACTION_VIEW” and pass the web url in the Uri and call setData() method.

 public static void OpenWebBrowser(Activity activity)
 {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.google.com"));
        activity.startActivity(intent);
 }
 

For invoking the web search you can use the “ACTION_VIEW” action and pass the search parameters in the URL for searching. Setup the appropriate query string for your search engine. There is another Action type “ACTION_WEB_SEARCH” provided by android to use google search. See the example below to open the search engine.

 public static void OpenWebSearch(Activity activity)
 {
        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
        intent.putExtra(android.app.SearchManager.QUERY, "brainy solutions org");
        activity.startActivity(intent);
 }
    

Create the intent having action type “ACTION_WEB_SEARCH” and pass the search query using putExtra() method. Pass name as “android.app.SearchManager.QUERY”. The name parameter takes the name of the extra data, with package prefix. Pass value as “brainy solutions org”. On the call of “OpenWebSearch(Activity activity)” Android studio launches the browser application and opens the search results in google search engine.

;