Hi all,In this tutorial I will show you how to open browser from an ANDROID application.
This is done through intents. You know that intents are used to invoke other activities.
So we here invoke the browser activity.
The following code will open the default browser in your ANDROID phone.

package com.myPackage;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class OpenURL extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String url = "http://www.google.com/";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
}

This is the manifest file for the above code.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.Open"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".OpenClass"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Please leave your valuable comments.

 

3 Comments

  1. Zamani June 22, 2012 at 5:50 pm

    I can browse , then select what i want , so how can i go back to my apps

     
    • James June 23, 2012 at 9:56 am

      Please explain your question? Didn’t really get it.

       
  2. lester February 17, 2013 at 3:44 pm

    Hi,
    I’m want to open a url from an icon and was hoping to use this snippet. I hace defined all my icons bu when I put the above code into the SDK I get the error: ‘The public type OpenURL must be defined in its own file;.

    Any thoughts

    Thanks.

     

Leave a Reply

Your email address will not be published. Required fields are marked *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.