Launch custom android application from android browser

后端 未结 16 1188
一个人的身影
一个人的身影 2020-11-21 06:02

Can anybody please guide me regarding how to launch my android application from the android browser?

相关标签:
16条回答
  • 2020-11-21 06:35

    Xamarin port of Felix's answer

    In your MainActivity, add this (docs: Android.App.IntentFilterAttribute Class):

    ....
    [IntentFilter(new[] { 
        Intent.ActionView }, 
        Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, 
        DataScheme = "my.special.scheme")
    ]
    public class MainActivity : Activity
    {
        ....
    

    Xamarin will add following in the AndroidManifest.xml for you:

    <activity
        android:label="Something"
        android:screenOrientation="portrait"
        android:theme="@style/MyTheme"
        android:name="blah.MainActivity">
        <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="my.special.scheme" />
        </intent-filter>
    </activity>
    

    And in order to get params (I tested in OnCreate of MainActivity):

    var data = Intent.Data;
    if (data != null)
    {
        var scheme = data.Scheme;
        var host = data.Host;
        var args = data.PathSegments;
    
        if (args.Count > 0)
        {
            var first = args[0];
            var second = args[1];
            ...
        }
    }
    

    As far as I know, above can be added in any activity, not only MainActivity

    Notes:

    1. When user click on the link, Android OS relaunch your app (kill prev instance, if any, and run new one), means the OnCreate event of app's MainLauncher Activity will be fired again.
    2. With this link: <a href="my.special.scheme://host/arg1/arg2">, in above last code snippet values will be:
    scheme: my.special.scheme
    host: host
    args: ["arg1", "arg2"]
    first: arg1
    second: arg2
    

    Update: if android creates new instance of your app, you should add android:launchMode="singleTask" too.

    0 讨论(0)
  • 2020-11-21 06:39

    As of 15/06/2019

    what I did is include all four possibilities to open url.

    i.e, with http / https and 2 with www in prefix and 2 without www

    and by using this my app launches automatically now without asking me to choose a browser and other option.

    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    
        <data android:scheme="https" android:host="example.in" />
        <data android:scheme="https" android:host="www.example.in" />
        <data android:scheme="http" android:host="example.in" />
        <data android:scheme="http" android:host="www.example.in" />
    
    </intent-filter>
    
    0 讨论(0)
  • 2020-11-21 06:43

    For example, You have next things:

    A link to open your app: http://example.com

    The package name of your app: com.example.mypackage

    Then you need to do next:

    1) Add an intent filter to your Activity (Can be any activity you want. For more info check the documentation).

            <activity
            android:name=".MainActivity">
    
            <intent-filter android:label="@string/filter_title_view_app_from_web">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <!-- Accepts URIs that begin with "http://example.com" -->
    
                <data
                    android:host="example.com"
                    android:scheme="http" />
            </intent-filter>
    
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    
        </activity> 
    

    2) Create a HTML file to test the link or use this methods.

    <a href="intent://example.com#Intent;scheme=http;package=com.example.mypackage;end">Open your Activity directly (just open your Activity, without a choosing dialog). </a>
    
    <a href="http://example.com">Open this link with browser or your programm (by choosing dialog).</a>
    

    3) Use Mobile Chrome to test

    4) That's it.

    And its not necessary to publish app in market to test deep linking =)

    Also, for more information, check documentation and useful presentation.

    0 讨论(0)
  • 2020-11-21 06:45

    Yeah, Chrome searches instead of looking for scheme. If you want to launch your App through URI scheme, use this cool utility App on the Play store. It saved my day :) https://play.google.com/store/apps/details?id=com.naosim.urlschemesender

    0 讨论(0)
  • 2020-11-21 06:45

    Felix's approach to handling deep links is the typical approach to handling deep links. I would also suggest checking out this library to handle the routing and parsing of your deep links:

    https://github.com/airbnb/DeepLinkDispatch

    You can use annotations to register your Activity for a particular deep link URI, and it will extract out the parameters for you without having to do the usual rigmarole of getting the path segments, matching it, etc. You could simply annotate and activity like this:

    @DeepLink("somePath/{someParameter1}/{someParameter2}")
    public class MainActivity extends Activity {
       ...
    }
    
    0 讨论(0)
  • 2020-11-21 06:47

    All above answers didn't work for me with CHROME as of 28 Jan 2014

    my App launched properly from http://example.com/someresource/ links from apps like hangouts, gmail etc but not from within chrome browser.

    to solve this, so that it launches properly from CHROME you have to set intent filter like this

    <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="example.com"
            android:pathPrefix="/someresource/"
            android:scheme="http" />
        <data
            android:host="www.example.com"
            android:pathPrefix="/someresource/"
            android:scheme="http" />
    </intent-filter>
    

    note the pathPrefix element

    your app will now appear inside activity picker whenever user requests http://example.com/someresource/ pattern from chrome browser by clicking a link from google search results or any other website

    0 讨论(0)
提交回复
热议问题