Starting a new activity with an intent object
The Android application model can be seen as a service-oriented one, with activities as components and intents as the messages sent between them. Here, an intent is used to start an activity that displays the user's call log, but intents can be used to do many things and we will encounter them throughout this book.
Getting ready
To keep things simple, we are going to use an intent object to start one of Android's built-in applications rather than create a new one. This only requires a very basic application, so start a new Android project with Android Studio and call it ActivityStarter
.
How to do it...
Again, to keep the example simple so that we can focus on the task at hand, we will create a function to show an intent in action and call this function from a button on our activity.
Once your new project is created in Android Studio, follow these steps:
- Open the
MainActivity.java
class and add the following function:public void launchIntent(View view) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("https://www.packtpub.com/")); startActivity(intent); }
While you are typing this code, Android Studio will give this warning on View and intent: Cannot resolve symbol 'Intent'.
This means that you need to add the library reference to the project. You can do this manually by entering the following code in the
import
section:import android.view.View; import android.content.Intent;
Alternatively, just click on the words (in the red font), hit Alt + Enter, and let Android Studio add the library reference for you.
- Open the
activity_main.xml
file and add the following XML:<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Launch Browser" android:id="@+id/button" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:onClick="launchIntent"/>
- Now it's time to run the application and see the intent in action. You will need to either create an Android emulator (in Android Studio, go to Tools | Android | AVD Manager) or connect a physical device to your computer.
- When you press the Launch Browser button, you will see the default web browser open with the URL specified.
How it works...
Though simple, this app demonstrates much of the power behind the Android OS. The intent object is just a message object. Intents can be used to communicate across your application's components (such as services and broadcast receivers) as well as with other applications on the device (as we did in this recipe).
Note
To test on a physical device, you may need to install drivers for your device (the drivers are specific to the hardware manufacturer). You will also need to enable Developer Mode on your device. Enabling Developer Mode varies according to the Android OS version. If you do not see the Developer Mode option in your device settings, open the About Phone option and begin tapping Build Number. After three taps, you should see a Toast message telling you that you are on your way to be a developer. Four more taps will enable the option.
In this recipe, we created an intent object by specifying ACTION_VIEW
as what we want to do (our intention). You may have noticed that when you typed Intent
and then the period, Android Studio provided a pop-up list of possibilities (this is the autocomplete feature), like this:
ACTION_VIEW
, along with a URL in the data, indicates that the intention is to view the website, so the default browser is launched (different data could launch different apps). In this example, our intent is just to view the URL, so we call the intent with just the startActivity()
method. There are other ways to call the intent depending on our needs. In the Returning a result from an activity recipe, we will use the startActivityForResult()
method.
There's more...
It's very common for Android users to download their favorite apps for web browsing, taking photos, text messaging, and so on. Using intents, you can let your app utilize your user's favorite apps instead of trying to reinvent all of this functionality.
See also
To start an activity from a menu selection, refer to the Handling menu selections recipe in Chapter 4, Menus.