Markup
<a href="javascript:void(0)" onclick="addToHomeScreen()">
点击这里,将本网页添加到主屏幕
</a>
<script>
function addToHomeScreen() {
var a2hsBtn = document.querySelector(".ad2hs-prompt")
a2hsBtn.style.display = "block"
a2hsBtn.addEventListener("click", addToHome)
}
function addToHome() {
var a2hsBtn = document.querySelector(".ad2hs-prompt")
a2hsBtn.style.display = "none"
var a2hsSkip = document.querySelector(".ad2hs-skip")
a2hsSkip.style.display = "none"
var a2hsBranding = document.querySelector(".ad2hs-branding")
a2hsBranding.style.display = "none"
var a2hsLauncher = document.querySelector(".ad2hs-launcher")
a2hsLauncher.style.display = "block"
deferredPrompt.prompt()
deferredPrompt.userChoice.then(function(choiceResult) {
deferredPrompt = null
})
}
</script>
这段代码包含了一个文本链接,用户点击该链接时会触发一个 JavaScript 函数,该函数会显示一个提示框,提示用户是否将网知慎页添加到主屏幕。用户确认后,该网页就会被添加到主屏幕上。
请注意,上述代码仅供参考,高虚您可能需要根据自己的需求进行修改。
JavaScript
<a href="javascript:void(0)" onclick="addToHomeScreen()">
点击这里,将本网页添加到主屏幕
</a>
<script>
function addToHomeScreen() {
var a2hsBtn = document.querySelector(".ad2hs-prompt")
a2hsBtn.style.display = "block"
a2hsBtn.addEventListener("click", addToHome)
}
function addToHome() {
var a2hsBtn = document.querySelector(".ad2hs-prompt")
a2hsBtn.style.display = "none"
var a2hsSkip = document.querySelector(".ad2hs-skip")
a2hsSkip.style.display = "none"
var a2hsBranding = document.querySelector(".ad2hs-branding")
a2hsBranding.style.display = "none"
var a2hsLauncher = document.querySelector(".ad2hs-launcher")
a2hsLauncher.style.display = "block"
deferredPrompt.prompt()
deferredPrompt.userChoice.then(function(choiceResult) {
deferredPrompt = null
})
}
</script>
如果您不是用户,而是程序员,您可以通过添加特定的 HTML 代码来让网页直接生成添加到手机主屏幕的代码。您可以参考上文中的代码示例,并进行相应的修改。
同时,您也可以使用 Web App Manifest 来为您的网页添加添加到主屏幕的功能。Web App Manifest 是一种 JSON 格式的文件,用于描述网页的一些基本信息,比如网页的名称、图标、启动方式等。您可以在网页的 head 标签中添加一个 link 标签,将 Web App Manifest 的 URL 指向您的网页。例如:
Java
<link rel="manifest" href="https://www.example.com/manifest.json">
JavaScript
然后,您可以在 manifest.json 文件中定义一些相关属性,以便描述网页。例如:
JavaScript
{
"name": "My Awesome Web App",
"short_name": "Awesome App",
"start_url": "./index.html",
"display": "standalone",
"background_color": "#fff",
"theme_color": "#fff",
"icons": [
{
"src": "./icon.png",
"sizes": "192x192",
"type": "image/png"
}
]
}
在这个示例中,我们为网页定义了一些基本属性,比如网页的名称、短名称、启动地址、显示方式等。通过这些属性,用户可以将网页添加到手机主屏幕上,并以“独立”模
非常好!如果您想尝试将网页添加到手机主屏幕,可以按照上述方法进行 *** 作。您可以先添加一些 HTML 代码,然后通过点击相应的按钮来触发添加到主屏幕的 *** 作。或者,您也可以使用 Web App Manifest 来描述网页,并让用户在访问网页时进行添加。
如果您遇到任何问题,可以随时联系我。愿您的尝试顺利!
1、手机一般都自带有浏览器,我们先打开浏览器,进入“书签”界面,点肢稿击“添加书签+”。2、输入网址点击前往就能打开网页,再点击浏山饥裤览器右上角的菜单。可以自行修改网页的名称,改好后点“储存”。回到“书签”界面,在“我的设备”下可以看到保存后的网页书签。
3、长按该网页书签,d出菜单,点击“添加快捷键”。回到桌面,就可以看逗简到网页书签生成了桌面图标,点击即可打开。
Android应用,创建桌面图标先在注孝历册巧枝搜activity时,需要添加一个action为android.intent.action.CREATE_SHOERTCUT的intentFilter。1.创建图标代码如下:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
</intent-filter>
</activity>
2.接下来就是就是设置快捷方式的图标、名称、事件等属性。
public void createShortCut(){
Intent addShortCut
if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){//判断是否需要添加快捷方式
addShortCut = new Intent()
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME , "快捷方式")//快捷方式的名称
Parcelable icon = ShortcutIconResource.fromContext(this, R.drawable.icon)//搭腔显示的图片
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon)//快捷方式激活的activity,需要执行的intent,自己定义
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent())
setResult(RESULT_OK, addShortCut)//OK,生成
}else{//取消
setResult(RESULT_CANCELED)
}
}
3.向桌面再增加一个图标
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT") //快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name))
shortcut.putExtra("duplicate", false)//不允许重复创建
//指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
//注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
// ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName())
// shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp))
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this,WXEntryActivity.class))//快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher)
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes)
sendBroadcast(shortcut)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)