In simple terms an intent is an object used to access a component from an activity. The component can be an activity or a service or a broadcast receiver. We can classify intents into two category such as explicit intents and implicit intents. In the case of explicit intent, the target component is accessed by its class name. On the other hand, in the case of implicit intent, the target component is accessed by action name, category and data. In this article we are going to create an application which access a target activity using explicit intent.
1. Create an Android project namely ExplicitIntentDemo
2. Select a build target for this application
3. Enter application details
4. Open the file res/values/strings.xml for adding new strings namely str_second_activity and str_btn_caption. After adding these strings, the file will look like as follows :
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">ExplicitIntentDemo</string> <string name="str_second_activity">Second Activity</string> <string name="str_btn_caption">Access Second Activity</string> </resources>
5. Create a layout file for the second activity. For this, create an Android XML file called second.xml in the directory res/layout/. Then open and update the file with the given below code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/str_second_activity" /> </LinearLayout>
6. Create an activity class for the second activity. For this, create a class file called SecondActivity.java in the directory src/in/wptrafficanalyzer/explicitintentdemo and update its content with the given below code
package in.wptrafficanalyzer.explicitintentdemo; import android.app.Activity; import android.os.Bundle; public class SecondActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); } }
7. Open the file AndroidManifest.xml for adding the newly created activity component to the application. Since this activity is accessed via an explicit intent, there is no need to define any intent filters for this activity. Because explicit intents are not being watched by any intent filters.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.wptrafficanalyzer.explicitintentdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <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> <activity android:name=".SecondActivity" android:label="@string/app_name" > </activity> </application> </manifest>
8. Open the layout file of the MainActivity ( res/layout/main.xml ) for adding new button. Clicking on this button will show the SecondActivity which we have created above. The updated main.xml file is given below
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn_access" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/str_btn_caption" /> </LinearLayout>
9. Create an explicit intent in the MainAcitivity. For this, open and update the file /src/in/wptrafficanalyzer/explicitintentdemo/MainActivity.java with the given below code
package in.wptrafficanalyzer.explicitintentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /** Getting a reference to the button in the main.xml **/ Button btn = (Button) findViewById(R.id.btn_access); /** Defining a click listener for the button **/ OnClickListener btnClickListener = new OnClickListener() { @Override public void onClick(View v) { /** Creating an explicit intent to access the SecondActivity **/ Intent intent = new Intent(getBaseContext(), SecondActivity.class); /** Starting the SecondActivity **/ startActivity(intent); } }; btn.setOnClickListener(btnClickListener); } }
10. Execute the application
11. Download the source code

12. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com