Android 浏览器启动应用程序
2024-10-18 08:36:08
点击浏览器中的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传递过来的值了。
最新文章
- Angularjs 双向绑定机制解析
- ready与onload的性能
- 诡异的 未处理的IOErrorEvent 2035
- ASP.NET MVC 网站开发总结(四)——校友平台开发总结
- Web 开发中应用 HTML5 技术的10个实例教程
- 转载:最大子段和问题(Maximum Interval Sum)
- Swift给每个开发者赢取500万的机会!不看一生后悔。
- poj 2661 Factstone Benchmark (Stirling数)
- python编程语言缩进格式
- win7下VS.NET中通过LinqToSQL连接oracle数据库
- Java基础知识强化之集合框架笔记47:Set集合之TreeSet保证元素唯一性和比较器排序的原理及代码实现(比较器排序:Comparator)
- linux 删除和安装java
- Java并发之CountDownLatch、CyclicBarrier和Semaphore
- Bootstrap各种进度条的实例讲解
- linux一台服务器配置多个Tomcat
- LR运行负载测试场景-笔记
- Java语法基础常见疑惑解答8,16,17,21图片补充
- 工程部署从tomcat6.0迁移到jboss6.0错误总结
- 我的notepad++
- 【线程】Volatile关键字