Using Intent to open a URL : Url « Network « Android






Using Intent to open a URL

 

package app.test;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

class MyBrowserActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.row);
    Uri url = getIntent().getData();
    WebView webView = (WebView) findViewById(R.id.WebView01);
    webView.setWebViewClient(new Callback());
    webView.loadUrl(url.toString());
  }
  private class Callback extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      return (false);
    }
  }
}
public class Test extends Activity {

  Button b1, b2, b3, b4, b5;
  int request_Code = 1;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b1 = (Button) findViewById(R.id.btn_webbrowser);
    b1.setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
        Intent i = new Intent("android.intent.action.VIEW");
        i.setData(Uri.parse("http://www.amazon.com"));
        startActivity(i);
      }
    });
    b2 = (Button) findViewById(R.id.btn_makecalls);
    b2.setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
        Intent i = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:+651234567"));
        startActivity(i);
      }
    });
    b3 = (Button) findViewById(R.id.btn_showMap);
    b3.setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
        Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri
            .parse("geo:37.827500,-122.481670"));
        startActivity(i);
      }
    });
    b4 = (Button) findViewById(R.id.btn_chooseContact);
    b4.setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
        Intent i = new Intent(android.content.Intent.ACTION_PICK);
        i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        startActivityForResult(i, request_Code);
      }
    });

    b5 = (Button) findViewById(R.id.btn_launchMyBrowser);
    b5.setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
        Intent i = new Intent("app.test.MyBrowser", Uri
            .parse("http://www.amazon.com"));
        i.addCategory("app.test.OtherApps");
        i.addCategory("app.test.SomeOtherApps");
        startActivity(i);
      }
    });
  }

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == request_Code) {
      if (resultCode == RESULT_OK) {
        Toast.makeText(this, data.getData().toString(),
            Toast.LENGTH_SHORT).show();
        Intent i = new Intent(android.content.Intent.ACTION_VIEW,
            Uri.parse(data.getData().toString()));
        startActivity(i);
      }
    }
  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<Button
    android:id="@+id/btn_webbrowser"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Web Browser" />    
<Button
    android:id="@+id/btn_makecalls"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Make Calls" />   
<Button
    android:id="@+id/btn_showMap"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Show Map" />    
<Button
    android:id="@+id/btn_chooseContact"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Choose Contact" />
<Button
    android:id="@+id/btn_launchMyBrowser"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Launch My Browser" />
    
</LinearLayout>
//row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<WebView 
    android:id="@+id/WebView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" />
</LinearLayout>

   
  








Related examples in the same category

1.Process xml document from a Url
2.Suggest Url Provider
3.Showing android:autoLink property, which linkifies things like URLs and phone numbers found in the text.
4.extends ArrayAdapter to create URL history
5.Used to compress URL using the bit.ly service.
6.URL encode and decode
7.Is valid URL
8.Download from URLConnection
9.Request from an URL
10.Get Url From Img Tag
11.Returns the contents of the given URL as an array of strings
12.Read from a URL
13.Pull the raw text content of the given URL.
14.Get Video from URL
15.Gets data from URL
16.get Url Response
17.URL Encode Utils
18.Downloads a file given URL to specified destination
19.get Host From Url
20.encode Url
21.decode Url
22.Convert a byte array to a URL encoded string
23.get Url content with max retries
24.get Url By Post
25.Take a base url and a {@link Map} of parameters to build a valid url