Android Learning Notes (18)-Use intent filters and implementations to browse the Web (with source code)

Source: Internet
Author: User

Using Intent Filters


Click to download the source code


1, create a intents project, add a new class to the project, named Mybrowseractivity, a new browser.xml under the Res/layout folder;

2. Add the following code to the Androidmanifest.xml file:

Add Permissions:

    <uses-permission android:name= "Android.permission.CALL_PHONE"/>    <uses-permission android:name= " Android.permission.INTERNET "/>
        <activity            android:name= ". Mybrowseractivity "            android:label=" @string/app_name ">            <intent-filter>                <action android: Name= "Android.intent.action.VIEW"/>                <action android:name= "Net.zenail.MyBrowser"/>                <category Android:name= "Android.intent.category.DEFAULT"/>                <data android:scheme= "http"/>            </ Intent-filter>        </activity>

Action: Activity; Category: Class; Data: Indicates the type of the obtained.

3. Add three buttons to the Main.xml file:

    <button        android:id= "@+id/btn_webbrowser"        android:layout_width= "fill_parent"        android:layout_ height= "Wrap_content"        android:onclick= "Onclickwebbrowser"        android:text= "Web Browser"/>    < Button        android:id= "@+id/btn_makecalls"        android:layout_width= "fill_parent"        android:layout_height= " Wrap_content "        android:onclick=" onclickmakecalls "        android:text=" make Calls "/>     <button        Android:id= "@+id/btn_launchmybrowser"        android:layout_width= "fill_parent"        android:layout_height= "wrap _content "        android:onclick=" Onclicklaunchmybrowser "        android:text=" Launch My Browser "/>

4, add three buttons corresponding to the three click Method in the Intentsactivity.java file:

public void Onclickwebbrowser (View v) {Intent Intent = new Intent (Android.content.intent.action_view,uri.parse ("http:/ /url "));//Enter Baidu URL here, csdn do not let add link ...//The advantage of using Createchooser ()://1, the title of the selection dialog that is displayed is changed, and there is no use by default for this action option//2, When there is no activity matching the program's intent object, the application does not crash//startactivity (Intent.createchooser (Intent, "Open URL using ...")); StartActivity ( Intent);} public void Onclickmakecalls (View v) {Intent Intent = new Intent (Android.content.intent.action_dial,uri.parse ("tel:+ 651234567 ")); StartActivity (intent);} public void Onclicklaunchmybrowser (View v) {Intent Intent = new Intent ("Net.zenail.MyBrowser"); Intent.setdata ( Uri.parse ("http://url"));//Enter Baidu website here, csdn do not let add link ... startactivity (intent);}

5. Add a WebView to the Browser.xml:

    <webview        android:id= "@+id/webview01"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"/>

6, add the following code in the Mybrowseractivity.java file, to realize the function of browsing the Web page:

public class Mybrowseractivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {//TODO Aut O-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (R.layout.browser); Uri URL = getintent (). GetData (); WebView WebView = (WebView) Findviewbyid (r.id.webview01); Webview.setwebviewclient (new Callback ()); Webview.loadurl ( Url.tostring ());} Private class Callback extends Webviewclient {@Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {//TODO auto-generated method Stubreturn false;}}}

7, run a bit, the effect is as follows:


Click a third button:


Click the first button:


To refine the intent filter, add the Createchooser () method to the Onclickwebbrowser () method in Intentsactivity.java:

StartActivity (Intent.createchooser (Intent, "Open URL using ..."));

The following effects are added:


Then you can select the application you want to select ~


Benefits of attaching and using Createchooser ():

1. Change the title of the display selection dialog, and there is no use by the default for this action option;

2. The application does not crash when there is no activity matching the intent object of the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.