Android Programming Implementation Click Link Open App feature example

Source: Internet
Author: User

The example in this article describes the Android programming implementation Click Link to open the app feature. Share to everyone for your reference, as follows:

Clicking on the link in Android to open the app is a very common requirement. For example, e-commerce will send a coupon to users often issued a text message: XXX coupon has been sent to your account, click on the XXX link to view! When the user clicks the link, it opens the local app and goes to the relevant page.

Feature implementation:

1. Add intent-filter to the corresponding activity in manifest:

?
123456789101112 <activity   android:name=".TestActivity">   <intent-filter>      <action android:name="android.intent.action.VIEW" />      <category android:name="android.intent.category.DEFAULT" />      <category android:name="android.intent.category.BROWSABLE" />      <data        android:host="mywebsite.com"        android:pathPrefix="/openApp"        android:scheme="http" />   </intent-filter></activity>

2. On the phone analog send a text message, including link http://mywebsite.com/openApp, mobile phone generally can automatically identify the link, click the link after the system will pop up the selection box, as follows:

Click on your app (androidtest), the system will automatically open Androidtest this program testactivity this page.

3. However, the above approach is obviously imperfect, as users will most likely choose the browser to open this link! To solve this problem, you can modify the scheme attribute to a custom one, for example:

?
1234 <data   android:host="mywebsite.com"   android:pathPrefix="/openApp"   android:scheme="myapp"/>

At this point, the corresponding link address is Myapp://mywebsite.com/openapp. Because only our own program can recognize the MyApp protocol, it will open the app directly. However, there are still problems:

(1) If you put the link on the page and want users to click on the link to open the app, then the above practice is not a problem. For example, add the following code to the Web page:

?
1 <ahref=‘myapp://mywebsite.com/openApp‘>点击打开APP</a>

(2) However, if you put the link in the text message will not be. Since MyApp does not recognize the SMS program of this protocol system, it is not marked as a link style, meaning the user cannot click directly.

The solution to this problem is to use the Web page redirection feature, such as sending a link in a text message: HTTP://ABC.COM/OPENAPP, and then adding a redirect to the page:

Copy CodeThe code is as follows: <meta http-equiv= "Refresh" content= "0;url=myapp://mywebsite.com/openapp? Name=zhangsan&age=20 "/>

When a user clicks on a text message, they open the link using the browser and then automatically open their app.

4. Finally, you can get the parameters of the link URL pass in testactivity:

?
12345678910 Intent intent = getIntent();String action = intent.getAction();if (Intent.ACTION_VIEW.equals(action)) {   Uri uri = intent.getData();   if (uri != null) {     String name = uri.getQueryParameter("name");     String age = uri.getQueryParameter("age");     Toast.makeText(this, "name=" + name + ",age=" + age, Toast.LENGTH_SHORT).show();   }}

PS: For Androidmanifest.xml file related properties, refer to the online tools:

Android manifest Features and permissions description Daquan:
Http://tools.jb51.net/table/AndroidManifest

Android Programming Implementation Click Link Open App feature example

Related Article

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.