zoukankan      html  css  js  c++  java
  • android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据

    http://itindex.net/blog/2014/11/07/1415353560000.html

    点击浏览器中的URL链接,启动特定的App。

    首先做成HTML的页面,页面内容格式如下:

    <a href="[scheme]://[host]/[path]?[query]">启动应用程序</a> 

    这一句就可以了。

    各个项目含义如下所示:

    scheme:判别启动的App。 ※详细后述

    host:适当记述

    path:传值时必须的key     ※没有也可以

    query:获取值的Key和Value  ※没有也可以

    作为测试好好写了一下,如下:

    <a href="myapp://jp.app/openwith?name=zhangsan&age=26">启动应用程序</a>  

    接下来是Android端。
    首先在AndroidManifest.xml的MAIN Activity下追加以下内容。(启动Activity时给予)

    ※必须添加项

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

    HTML记述的内容加入<data …/>。
    其中必须的内容仅scheme,没有其他内容app也能启动。

    ※注意事项:intent-filter的内容【android.intent.action.MAIN】和 【android.intent.category.LAUNCHER】这2个,不能与这次追加的内容混合。
                     所以,如果加入了同一个Activity,请按以下这样做,否则会导致应用图标在桌面消失等问题。

    复制代码
    <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> 
    复制代码

    这样的话,没有问题。

    接下来在Activity中需要取值的地方添加以下代码,我是直接写在OnCreate函数里的:

     
    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");  
        }  
    }
     

    这样就能获取到URL传递过来的值了。

  • 相关阅读:
    写一个列表生成式,产生一个公差为11的等差数列
    如果对方网站反爬取,封IP了怎么办?
    为什么会选择redis数据库?
    你是否了解谷歌的无头浏览器?
    遇到的反爬虫策略以及解决方法?
    常见的HTTP方法有哪些?
    遇到反爬机制怎么处理?
    列举网络爬虫所用到的网络数据包,解析包?
    python中的关键字yield有什么作用?
    如下代码输出的是什么?
  • 原文地址:https://www.cnblogs.com/wanqieddy/p/4661848.html
Copyright © 2011-2022 走看看