How to implement Android by clicking the link in the browser to open apk

Source: Internet
Author: User

intent://scan/#Intent; scheme=appname://appname/"channel"/"id";p ackage=com.appname.package;end

First make the HTML page, the page content format is as follows:

<a href= "[Scheme]://[host]/[path]? [query] > Start Application </a>

This sentence will be all right.

The meanings of each item are as follows:

Scheme: Identify the app that starts. ※ The following details are described

Host: Proper description

Path: The key※ required to pass the value is not also available

Query: Get the value of key and Value※ no also can

Write it well as a test, as follows:

<a href= "myapp://jp.app/openwith?name=zhangsan&age=26" > Start Application </a>

Next comes the Android side.

First, add the following to the main activity of Androidmanifest.xml. (given when activating activity)

* Items must be added

<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:scheme= "MyApp" android:host= "Jp.app" android:pathprefix= "/openwith"/>

</intent-filter>

The contents of the HTML description are added to <data .../>.

The required content is only scheme, and no other content app can be launched.

※ Note: The contents of Intent-filter "Android.intent.action.MAIN" and "Android.intent.category.LAUNCHER" are 2, and cannot be mixed with this additional content.

So, if you join the same activity, do this as follows, or it will cause the app icon to disappear on the desktop.

Copy Code

<intent-filter>

<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>

</intent-filter>

<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:scheme= "MyApp" android:host= "Jp.app" android:pathprefix= "/openwith"/>

</intent-filter>

Copy Code

In that case, no problem.

Next, add the following code where the activity needs to be valued, and I'll write it directly in the OnCreate function:

Intent I_getvalue = Getintent ();

String action = I_getvalue.getaction ();

if (Intent.ACTION_VIEW.equals (ACTION)) {

Uri uri = I_getvalue.getdata ();

if (uri! = null) {

String name = Uri.getqueryparameter ("name");

String age= uri.getqueryparameter ("Age");

}

}

This will get the value passed to the URL.

—————————————————————————————————— I'm a split line ————————————————————————————————————

Code copy is finished, is not very surprised to find in the browser input

Myapp://jp.app/openwith?name=zhangsan&age=26

Is it 404, not open?

Landlord you This is not a lie! Lou Zhu, you son of a.

Sir, don't be impatient, do you see what browser you are using? UC, Cheetah, open gate? Give up, try the system comes with a browser or Google browser bar. Must be able to succeed, can not succeed again to pit me. Ha ha.

—————————————————————————————————— I'm a split line ————————————————————————————————————

Suddenly feel good sad, finally get this skill, but can not be used by third-party browser. It's a tragedy that most of the Android browsers are occupied by third parties.

Then let's talk about why third-party browsers don't succeed. First of all, I found the UC browser, and if you use your own scheme instead of HTTP, UC will default to adding http://In front of your scheme. This is too much of a dad. Other browsers don't see the same situation. After discovering the problem, I tried to change my scheme to HTTP. Then ran again with anticipation, and the result was a pit-father. So I think it would be a third-party browser to address the URL. Here, I have no alternative. I tested UC, cheetah, open Gate, these 3 are not supported. The system comes with and Google Chrome is supported.

Finally, add a clue, in the browser search Baidu application. After entering their pages, they can be implemented in various browsers to launch already installed local apps. After seeing this, I read the source code of their page.

Here they add a data-sentintent label, see here, should be able to determine that the third-party browser should be the default does not support hair intent, only one. According to the front end, this tag should be customized. When we look at the source at the front, we find this.

So the final result should be the Baidu side is a port, and then in the application to enable a service, to listen to this port, to obtain this intent. That's probably the idea. But the landlord did not actually go to operate. Project time is tight, too troublesome.

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------

 Use an<intent-filter> with a <data> element. For example, to handle all links to twitter.com, you ' d put this inside your <activity> your androidmanifest.xml:& lt;intent-filter> <data android:scheme= "http" android:host= "twitter.com"/> <action android:name= "Androi D.intent.action.view "/></intent-filter>Then if the user clicks on a link to Twitter in the browser, they'll be is asked what application to use in order to Complete the Action:the browser or your application. Of course,ifYou want to provide tight integration between your website and your apps, you can define your own scheme:<intent-filter> <data android:scheme= "My.special.scheme"/> <action android:name= "android.intent.act Ion. VIEW "/></intent-filter>Then , the in your web app can put the links like:<a href= "My.special.scheme://other/parameters/here" >and when the user clicks it, your app would be launched automatically (because it'll probably be the only one that CA N Handle My.special.scheme://type of URIs). The only downside to the if the user doesn ' t has the app installed, they ' ll get a nasty error. And I ' m not sure there's any-to-check.Edit:to answer your question, you can use Getintent (). GetData () which returns a Uri object. You can then use the Uri.* Methods to extract the data you need. For example, let's say the user clicked on a link to http://twitter.com/status/1234:Uri Data=getintent (). GetData (); String Scheme= Data.getscheme ();//"http"String host = Data.gethost ();//"Twitter.com"list<string> params =data.getpathsegments (); String First= Params.get (0);//"Status"String second = Params.get (1);//"1234"You can DoThe above anywhere in your Activity, but you ' re probably going to want to do it in OnCreate (). You can also with params.size () to get the number of the path segments in the Uri. Look to Javadoc or the Android developer website for other URIs methods you can use to extract specific parts.

How to implement Android by clicking the link in the browser to open apk

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.