首页 文章

从Android浏览器启动自定义Android应用程序

提问于
浏览
385

任何人都可以指导我如何从Android浏览器启动我的Android应用程序?

15 回答

  • 18

    以下链接提供有关直接从浏览器启动应用程序(如果已安装)的信息 . 否则它会直接在Play商店中打开应用程序,以便用户可以无缝下载 .

    https://developer.chrome.com/multidevice/android/intents

  • 592

    是的,Chrome搜索而不是寻找方案 . 如果要通过URI方案启动应用程序,请在Play商店中使用此酷实用程序App . 它救了我的一天:) https://play.google.com/store/apps/details?id=com.naosim.urlschemesender

  • 66

    <intent-filter><data>元素一起使用 . 例如,要处理twitter.com的所有链接,你可以将它放在 AndroidManifest.xml 的_1786815中:

    <intent-filter>
        <data android:scheme="http" android:host="twitter.com"/>
        <action android:name="android.intent.action.VIEW" />
    </intent-filter>
    

    然后,当用户在浏览器中单击指向twitter的链接时,将询问他们使用哪个应用程序来完成操作:浏览器或您的应用程序 .

    当然,如果您想在您的网站和应用程序之间提供紧密集成,您可以定义自己的方案:

    <intent-filter>
        <data android:scheme="my.special.scheme" />
        <action android:name="android.intent.action.VIEW" />
    </intent-filter>
    

    然后,在您的网络应用程序中,您可以添加以下链接:

    <a href="my.special.scheme://other/parameters/here">
    

    当用户点击它时,您的应用程序将自动启动(因为它可能是唯一可以处理 my.special.scheme:// 类型的uris的应用程序) . 唯一的缺点是,如果用户没有得到一个令人讨厌的错误 . 而我'm not sure there'的任何方式来检查 .


    Edit: 要回答您的问题,您可以使用 getIntent().getData() 返回Uri对象 . 然后,您可以使用 Uri.* 方法提取所需的数据 . 例如,假设用户点击了 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"
    

    您可以在 Activity 中的任何位置执行上述操作,但您可能希望在 onCreate() 中执行此操作 . 您还可以使用 params.size() 获取 Uri 中的路径段数 . 查看javadoc或android开发人员网站,了解可用于提取特定部分的其他 Uri 方法 .

  • 3

    请在此处查看我的评论:Make a link in the Android browser start up my app?

    我们强烈反对人们不使用他们自己的计划,除非他们正在定义一个新的全球互联网计划 .

  • 0

    截至2014年1月28日,所有上述答案对我来说都不适用 CHROME

    我的应用程序从诸如环聊,gmail等应用程序的http://example.com/someresource/链接正确启动,但不是从Chrome浏览器中启动 .

    要解决这个问题,以便从CHROME正确启动你必须像这样设置intent过滤器

    <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>
    

    注意 pathPrefix 元素

    只要用户通过点击谷歌搜索结果或任何其他网站上的链接从Chrome浏览器请求http://example.com/someresource/模式,您的应用就会显示在活动选择器中

  • 8

    在我的情况下,我必须为 <intent-filter> 设置两个类别然后它工作:

    <intent-filter>
    <data android:scheme="my.special.scheme" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>
    
  • 6

    还应该在意图过滤器中添加 <category android:name="android.intent.category.BROWSABLE"/> ,以便从链接中正确识别活动 .

  • 6

    例如,你有接下来的事情:

    打开应用程序的链接:http://example.com您的应用程序的包名称:com.example.mypackage

    然后你需要做下一步:

    1)为您的活动添加一个意图过滤器(可以是您想要的任何活动 . 有关更多信息,请查看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)创建一个HTML文件来测试链接或使用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)使用Mobile Chrome进行测试

    4)就是这样 .

    并没有必要在市场上发布应用程序以测试深层链接=)

    另外,有关更多信息,请查看documentation并使用presentation .

  • 0

    请注意,当您实现此功能时,如果您的图标从Android启动器中消失,则必须拆分intent-filter .

    <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <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="your-own-uri" />
            </intent-filter>
        </activity>
    
  • 2

    Felix处理深层链接的方法是处理深层链接的典型方法 . 我还建议检查这个库来处理深层链接的路由和解析:

    https://github.com/airbnb/DeepLinkDispatch

    您可以使用注释为特定的深层链接URI注册您的活动,它将为您提取参数,而无需执行获取路径段,匹配它等的常规操作 . 您可以简单地注释和活动,如此:

    @DeepLink("somePath/{someParameter1}/{someParameter2}")
    public class MainActivity extends Activity {
       ...
    }
    
  • 0

    Xamarin Felix 的回答

    MainActivity 中,添加此文档(docs:Android.App.IntentFilterAttribute Class):

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

    Xamarin将在_1786859中为您添加以下内容:

    <activity android:label="Something" android:screenOrientation="portrait" android:theme="@style/MyTheme" android:name="blahblahblah.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>
    

    并且为了获得params(我在MainActivity的OnCreate中测试过):

    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];
            ...
        }
    }
    

    据我所知,上面的内容可以添加到任何活动中,而不仅仅是MainActivity

  • 18

    嘿,我得到了解决方案 . 我没有将类别设置为“默认” . 我也是使用主要活动作为意图数据 . 现在我对意图数据使用不同的活动 . 谢谢您的帮助 . :)

  • 0

    您需要向CALLBACK_URL'app添加一个伪主机名://'作为URL没有意义,无法解析 .

  • 47

    使用example.php:

    <?php
    if(!isset($_GET['app_link'])){  ?>   
        <iframe src="example.php?app_link=YourApp://blabla" style="display:none;" scrolling="no" frameborder="0"></iframe>
        <iframe src="example.php?full_link=http://play.google.com/xyz" style="display:none;" scrolling="no" frameborder="0"></iframe>
        <?php 
    }
    else { ?>
        <script type="text/javascript">
        self.window.location        = '<?php echo $_GET['app_link'];?>';
        window.parent.location.href = '<?php echo $_GET['full_link'];?>';
        </script>
    <?php 
    }
    
  • 64

    here中查看@JRuns的答案 . 我们的想法是使用您的自定义方案创建html并将其上传到某处 . 然后,如果您单击html文件上的自定义链接,您将被重定向到您的应用程序 . 我用this文章为android . 但是别忘了将全名 Name = "MyApp.Mobile.Droid.MainActivity" 属性设置为目标活动 .

相关问题