服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android中创建快捷方式及删除快捷方式实现方法

Android中创建快捷方式及删除快捷方式实现方法

2021-03-25 14:58安卓开发网 Android

这篇文章主要介绍了Android中创建快捷方式及删除快捷方式实现方法,本文直接给出实现代码,需要的朋友可以参考下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
     *
     * 创建快捷方式
     * @param map 快捷方式图标
     * @param appName 快捷方式标题
     * @param appUrl 快捷方式打开的地址
     * @param iconUrl 快捷方式图标地址
     *
     * */
    public static void createShortcut(Context activity ,Bitmap map ,String appName ,String appUrl ,String iconUrl){
        Intent shortcut = new Intent(
                "com.android.launcher.action.INSTALL_SHORTCUT");
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,appName);
        shortcut.putExtra("duplicate", false);// 设置是否重复创建
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW) ;
//      intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ;
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) ;
        intent.setClass(activity, WebViewActivity.class);// 设置第一个页面
        intent.putExtra("keyword", appUrl);
        intent.putExtra("appName", appName) ;
        intent.putExtra("iconUrl", iconUrl) ;
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, map);
        activity.sendBroadcast(shortcut);      
    }
    /**
     *
     * 删除快捷方式
     * @param shortcutName app名字
     * @param className 绝对路径如:getPackageName() + ".WebViewActivity"
     *
     * */
    public static void removeShortcut(Context cxt, String shortcutName, String className) {
    Intent shortcutIntent = new Intent(Intent.ACTION_VIEW);
    shortcutIntent.setClassName(cxt, className);
    Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
    cxt.sendBroadcast(intent);
  }

延伸 · 阅读

精彩推荐