hml5+ Android创建快捷方式

<!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">
			/*
 * 		首先是供参考对比的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);
				console.log("桌面快捷方式已创建完成!");
			}

			/*
			 * 		供参考对比的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);
				console.log("桌面快捷方式已删除!");
			}

		</script>

		<style type="text/css">

		</style>
	</head>

	<body>
		<header id="header">
			<div class="nvbt iback" onclick="back();"></div>
			<div class="nvtt">Shortcut</div>
		</header>
		<div id="dcontent" class="dcontent">
			<br />
			<div class="button" onclick="createShortcut()">创建桌面快捷方式</div>
			<div class="button" onclick="deleteShortcut()">删除桌面快捷方式</div>
			<br />
			<br />
			<p class="des">
				快捷方式操作是通过向Android系统发送广播的方式来实现,可能在一些定制的ROM上没有实现此类广播的处理,导致无法创建桌面快捷方式。
			</p>
			<br />
		</div>
		<div id="output">
			Native.JS for Android,可通过plus.android.*调用几乎所有的系统API。
		</div>
	</body>

</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章