您的位置:首页 > 移动开发 > Android开发

android 外部html页面启动本地app及app启动问题

2016-05-16 16:51 555 查看
外部html界面启动本地app

http://my.oschina.net/liucundong/blog/354029?fromerr=KocmE0mO

<pre name="code" class="html"><!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black"/>

<title>this's a demo</title>
<meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,minimal-ui">
</head>
<body>
<div>
<a id="J-call-app" href="javascript:;" class="label">立即打开>></a>
<input id="J-download-app" type="hidden" name="storeurl" value="http://m.chanyouji.cn/apk/chanyouji-2.2.0.apk">
</div>

<script>
(function(){
var ua = navigator.userAgent.toLowerCase();
var t;
var config = {
/*scheme:必须*/
scheme_IOS: 'cundong://',
scheme_Adr: 'cundong://splash',
download_url: document.getElementById('J-download-app').value,
timeout: 600
};

function openclient() {
var startTime = Date.now();

var ifr = document.createElement('iframe');

ifr.src = ua.indexOf('os') > 0 ? config.scheme_IOS : config.scheme_Adr;
ifr.style.display = 'none';
document.body.appendChild(ifr);

var t = setTimeout(function() {
var endTime = Date.now();

if (!startTime || endTime - startTime < config.timeout + 200) {
window.location = config.download_url;
} else {

}
}, config.timeout);

window.onblur = function() {
clearTimeout(t);
}
}
window.addEventListener("DOMContentLoaded", function(){
document.getElementById("J-call-app").addEventListener('click',openclient,false);

}, false);
})()
</script>
</body>
</html>



AndroidMainfext.xml

<activity
android:name=".activity.LauncherActivity"
android:configChanges="orientation|keyboardHidden|navigation|screenSize"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<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:host="splash" android:scheme="cundong" />
</intent-filter>
</activity>
外部html启动本地app之后发现出了问题,如果启动之前那个app已经打开了,那么启动就会导致有些fragment加载不出来了,
后来在启动界面设置启动模式为singleInstance,启动就正常了,但又出现了一个问题,这个启动模式是新创建了一个栈,那么按home键app回到后台后,再点击桌面图标进入app的时候启动界面就跑出来了,然后才显示刚才打开的界面。又百度了一下,发现http://blog.csdn.net/busjb/article/details/40891239</span>
在启动界面添加了代码就正常了
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
//结束你的activity
finish();
return;
}

但测试的时候又发现了新的问题,app没有启动,在html界面启动,那么按home返回桌面,再在桌面启动,fragment又不显示了,无意中发现手机当前的

应用列表里竟然没有这个app,但这个app分明是运行了,我完全不知道怎么回事。

于是随便搞搞,把启动activity的启动模式设置成singTast 就可以了,虽然我还是不知道为什么



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: