| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 | <!DOCTYPE HTML><html>	<head>		<meta charset="utf-8"/>		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>		<meta name="HandheldFriendly" content="true"/>		<meta name="MobileOptimized" content="320"/>		<title>Hello H5+</title>		<script type="text/javascript" src="../js/common.js"></script>		<script type="text/javascript">/* * 		首先是供参考对比的Android源代码,用Android的java代码创建手机桌面快捷方式 * 	import android.content.Intent;	import android.graphics.BitmapFactory;	import android.graphics.Bitmap;	import android.app.Activity;	// 获取主Activity	Activity main = this;	// 创建快捷方式意图	Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");	// 设置快捷方式的名称	shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloH5+");	// 设置不可重复创建	shortcut.putExtra("duplicate",false);	// 设置快捷方式图标	Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/icon.png");	shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);	// 设置快捷方式启动执行动作	Intent action = new Intent(Intent.ACTION_MAIN);	action.setComponent( main.getComponentName() );	shortcut.putExtra( Intent.EXTRA_SHORTCUT_INTENT, action );	// 广播创建快捷方式	context.sendBroadcast(shortcut);*//* * 如下开始是HTML5plus的js代码,演示如何通过js桥技术调用Android的原生api,从而完成手机桌面快捷方式创建。 */var Intent=null,BitmapFactory=null;var main=null;// H5 plus事件处理function plusReady(){	if ( plus.os.name == "Android" ) {		// 导入要用到的类对象		Intent = plus.android.importClass("android.content.Intent");		BitmapFactory = plus.android.importClass("android.graphics.BitmapFactory");		// 获取主Activity		main = plus.android.runtimeMainActivity();	}}if(window.plus){	plusReady();}else{	document.addEventListener("plusready",plusReady,false);}// DOMContentLoaded事件处理var _domReady=false;document.addEventListener("DOMContentLoaded",function(){	eStart = document.getElementById( "start" );},false);/** * 创建桌面快捷方式 */function createShortcut(){	// 创建快捷方式意图	var shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");	// 设置快捷方式的名称	shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloH5+");	// 设置不可重复创建	shortcut.putExtra("duplicate",false);	// 设置快捷方式图标	var iconPath = plus.io.convertLocalFileSystemURL("_www/icon.png"); // 将相对路径资源转换成系统绝对路径	var bitmap = BitmapFactory.decodeFile(iconPath);	shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,bitmap);	// 设置快捷方式启动执行动作	var action = new Intent(Intent.ACTION_MAIN);	//action.setComponent(main.getComponentName());	action.setClassName(main.getPackageName(), 'io.dcloud.PandoraEntry');	shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,action);	// 广播创建快捷方式	main.sendBroadcast(shortcut);	outSet( "桌面快捷方式已创建完成!" );}/* * 		供参考对比的Android源代码,用Android的java代码删除手机桌面快捷方式 *  * 		// 获取主Activity * 		Activity main = this; * 		// 创建快捷方式意图 * 		Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); * 		// 设置快捷方式的名称 * 		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"HelloH5+"); * 		// 设置快捷方式启动执行动作 * 		Intent action = new Intent(Intent.ACTION_MAIN); * 		action.setComponent(main.getComponentName()); * 		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,action); * 		// 广播删除快捷方式 * 		main.sendBroadcast(shortcut); *//* * 删除桌面快捷方式 */function deleteShortcut(){	// 创建快捷方式意图	var shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");	// 设置快捷方式的名称	shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"HelloH5+");	// 设置快捷方式启动执行动作	var action = new Intent(Intent.ACTION_MAIN);	//action.setComponent(main.getComponentName());	action.setClassName(main.getPackageName(), 'io.dcloud.PandoraEntry');	shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,action);	// 广播创建快捷方式	main.sendBroadcast(shortcut);	outSet( "桌面快捷方式已删除!" );}		</script>		<link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>		<style type="text/css">				</style>	</head>	<body>		<br/>		<div class="button" onclick="createShortcut()">创建桌面快捷方式</div>		<div class="button" onclick="deleteShortcut()">删除桌面快捷方式</div>		<br/>		<br/>		<p class="des">			快捷方式操作是通过向Android系统发送广播的方式来实现,可能在一些定制的ROM上没有实现此类广播的处理,导致无法创建桌面快捷方式。		</p>		<br/>		<div id="outpos"/>		<div id="output">Native.JS for Android,可通过plus.android.*调用几乎所有的系统API。		</div>	</body></html>
 |