Introduction

In the last article How to Open New Activity on Click Button by Existing Activity In Android, we saw how to communicate with a new activity. You know the intent is divided into the three parts Action, Data and Category. In general, an Action is something to be performed, such as the action view, action edit and action main. And Data is something to be operated on, such as a personal record, contact database, or Uri.
Today I will explain how to use an Intent in an online application in Android.
Step 1
Create a new project using "File" -> "New" -> "Android Application Project" and name it "IntentExample" as shown below.
6.1new.jpg
Step 2
Open "activity_main.xml" as "IntentExample/res/layout/activity_main.xml" and update it as in the following:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context="com.intent.example.MainActivity"
  6. android:layout_gravity="center">
  7. <Button
  8. android:id="@+id/btn"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:onClick="openBrowser"
  12. android:text="@string/browser_btn" >
  13. </Button>
  14. </RelativeLayout>
Step 3
Open "string.xml" as "IntentExample/res/values/string.xml" and update it as shown in the following:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">IntentExample</string>
  4. <string name="hello_world">Hello world!</string>
  5. <string name="menu_settings">Settings</string>
  6. <string name="browser_btn">Open</string>
  7. </resources>
Step 4
Open "MainActivity.java" as "IntentExample/src/com.intent.example" and update it with your logic; whatever you want to do.
  1. package com.intent.example;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.View;
  8. public class MainActivity extends Activity {
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. }
  14. public void openBrowser(View view){
  15. Intent i = new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com"));
  16. startActivity(i);
  17. }
  18. @Override
  19. public boolean onCreateOptionsMenu(Menu menu) {
  20. // Inflate the menu; this adds items to the action bar if it is present.
  21. getMenuInflater().inflate(R.menu.activity_main, menu);
  22. return true;
  23. }
  24. }
Step 5
Open the Android manifest file using "IntentExample/mainfest.xml" and if you want to update the related permission or add a new activity UI then update it as shown below:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.intent.example"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="17" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.intent.example.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 6
Run the application and see the output as shown below.
Click on the "Open" button:
6out1.jpg
And see:
6out2.jpg