The Intent Object

3
the intent object, which is often used in conjunction with activities. Intents can be used to transfer data between activities in your own application, as well as in external applications, such as those included with the Android operating system (a common example would be to use an intent to launch the default web browser). <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name= "android.intent.category.LAUNCHER"/> </intent-filter> </activity> Declaring an activity is a simple matter of declaring the <activity> element and specifying the name of the activity class with the android:name attribute. By adding the <activity> element to the Android Manifest, we are specifying our intention to include this component within our application. Any activities (or any other component for that matter) that are not declared in the manifest will not be included in the application. Attempting to access or utilize an undeclared component will result in an exception being thrown at runtime. In the preceding code, there is another attribute—android:label. This attribute indicates the title shown on the screen as well as the icon label if this is the Launcher activity. Single activity calling intent 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: 1. 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'.

description

Android intent

Transcript of The Intent Object

the intent object, which is often used in conjunction with activities. Intents can be used to transfer data between activities in your own application, as well as in external applications, such as those included with the Android operating system (a common example would be to use an intent to launch the default web browser).

<activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name= "android.intent.category.LAUNCHER"/> </intent-filter>

</activity>

Declaring an activity is a simple matter of declaring the <activity> element and specifying the name of the activity class with the android:name attribute. By adding the <activity> element to the Android Manifest, we are specifying our intention to include this component within our application. Any activities (or any other component for that matter) that are not declared in the manifest will not be included in the application. Attempting to access or utilize an undeclared component will result in an exception being thrown at runtime. In the preceding code, there is another attribute—android:label. This attribute indicates the title shown on the screen as well as the icon label if this is the Launcher activity.

Single activity calling intentwe 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: 1. 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.

2. 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"/>

[[http://www.mkyong.com/android/android-how-to-center-button-on-screen/

A small Android tip to show you how to center button on screen. Wrap button in RelativeLayout, and set following attributes to “true“.

Markup

android:layout_centerVertical="true"

android:layout_centerHorizontal="true"

]]

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. 3. When you press the Launch Browser button, you will see the default web browser open

with the URL specified. 4. 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: 5. 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. Like while Returning a result from an activity, we will use the startActivityForResult() method.

==

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

==