Android Studio phase 64th-Android Business component URL scheme use

Source: Internet
Author: User

What is URL Scheme?

The scheme in Android is a kind of in-page jump protocol, is a very good implementation mechanism, by defining their own scheme protocol, can be very convenient to jump to the various pages of the app, through the scheme protocol, the server can be customized to tell the app to jump that page, You can customize the jump page via the notification bar message, which can be redirected through the H5 page.

URL Scheme Application scenario:

A client app can register a URL scheme with the operating system that is used to launch the app from a browser or other app. The specified URL field allows the app to open certain pages directly after it has been called, such as the Product Details page, the Activity Details page, and so on. You can also perform certain actions, such as completing payments, and so on. You can also directly invoke a page within the app by using an HTML page. The URL scheme usage scenario is broadly divided into the following categories:

    • Server issued a jump path, the client according to the server issued a jump path to jump to the corresponding page

    • H5 page Click on the anchor point, depending on the anchor point specific jump path app side jump specific page

    • The app side receives the server-side push notification bar message and jumps to the relevant page according to the message's Click Jump Path.

    • The app jumps to another app-specific page based on the URL

URL Scheme protocol format:

First, a full URL scheme protocol format:

xl://goods:8888/goodsdetail?goodsid=10011002

Through the above path Scheme, Host, port, path, query all contained, basically the use of the path is usually the case.

    • XL represents the scheme agreement name

    • Goods represents which address field the scheme acts on

    • Goodsdetail represents the page specified by scheme

    • Parameters passed by Goodsid delegates

    • 8888 The port number that represents the path

how URL scheme is used:1.) Add <intent-filter/> Set scheme to <activity/> tag in androidmanifest.xml


<activity android:name= ". Goodsdetailactivity "
Android:theme= "@style/apptheme" >
<!--to be able to successfully tune apps on another app, you must add a intent filter--
<intent-filter>
<!--protocol section, whatever you want to set--
<data android:scheme= "XL" android:host= "goods" android:path= "/goodsdetail" android:port= "8888"/>
<!--The following lines also have to be set--
<category android:name= "Android.intent.category.DEFAULT"/>
<action android:name= "Android.intent.action.VIEW"/>
<category android:name= "Android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>


2.) Get the parameters of the scheme jump


Uri uri = getintent (). GetData (); if (uri! = null) {//Full URL information
String URL = uri.tostring ();
LOG.E (TAG, "url:" + uri); Scheme section
String scheme = Uri.getscheme ();
LOG.E (TAG, "scheme:" + scheme); Host Section
String host = Uri.gethost ();
LOG.E (TAG, "host:" + host); Port section
int port = Uri.getport ();
LOG.E (TAG, "host:" + port); Access to road strength
String path = Uri.getpath ();
LOG.E (TAG, "path:" + path);
list<string> pathsegments = uri.getpathsegments (); Query section
String query = Uri.getquery ();
LOG.E (TAG, "query:" + query); Gets the specified parameter value
String Goodsid = Uri.getqueryparameter ("Goodsid");
LOG.E (TAG, "Goodsid:" + goodsid);
}


3.) Call Mode

On the Web page

<a href= "xl://goods:8888/goodsdetail?goodsid=10011002" > Open Item Details </a>

Native invocation

Intent Intent = new Intent (Intent.action_view,uri.parse ("xl://goods:8888/goodsdetail?goodsid=10011002"));
StartActivity (Intent);
4.) How to determine if a scheme is valid


Packagemanager Packagemanager = Getpackagemanager ();
Intent Intent = new Intent (Intent.action_view, Uri.parse ("xl://goods:8888/goodsdetail?goodsid=10011002"));
List<resolveinfo> activities = packagemanager.queryintentactivities (intent, 0); Boolean isValid =! Activities.isempty (); if (isValid) {
StartActivity (Intent);
}


Summary:

The basic use of scheme is so much, the other uses in the later time to do a summary.



Android Studio phase 64th-Android Business component URL scheme use

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.