在主屏幕中创建快捷方式 [英] Create a shortcut in the homescreen

查看:174
本文介绍了在主屏幕中创建快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户选择添加主屏幕快捷方式来启动应用程序(在第一次启动时提示他)。

I want to give the user the option of add a homescreen shortcut to launch the app (prompting him at the first launch).

我看到了例如Whatsapp自动创建该图标

I saw that for example Whatsapp create that icon automatically

我该怎么办?该应用是否需要其他权限?谢谢

How can I do it? Does the app require additional permissions? Thanks

我正在尝试(但不起作用)

Im trying with (but doesn't work)

    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

    // Shortcut name
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyApp");  
    shortcut.putExtra("duplicate", false);  // Just create once

    // Setup current activity shoud be shortcut object 
    ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());  
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));  

    // Set shortcut icon
    ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);  
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);  

    sendBroadcast(shortcut);  


推荐答案

您是否在清单文件中设置了活动意图?在这里阅读:

Did you set the activity intent in your manifest file? read here:

Android在主屏幕上创建快捷方式

这篇关于在主屏幕中创建快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆