首页 > 移动平台 > 详细

Android -- 创建桌面快捷方式

时间:2014-08-14 13:58:58      阅读:291      评论:0      收藏:0      [点我收藏+]

代码                                                                                   

/**
     * 
     * 返回添加到桌面快捷方式的Intent:
     * 
     * 1.给Intent指定action="com.android.launcher.INSTALL_SHORTCUT"
     * 
     * 2.给定义为Intent.EXTRA_SHORTCUT_INENT的Intent设置与安装时一致的action(必须要有)
     * 
     * 3.添加权限:com.android.launcher.permission.INSTALL_SHORTCUT
     */

    public Intent getShortcutToDesktopIntent(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, context.getClass());
        /* 以下两句是为了在卸载应用的时候同时删除桌面快捷方式 */
        intent.setAction("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");

        Intent shortcut = new Intent(
                "com.android.launcher.action.INSTALL_SHORTCUT");
        // 不允许重建
        shortcut.putExtra("duplicate", false);
        // 设置名字
        // shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "11111");
        // 设置图标
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(context,
                        R.drawable.youtube));
        // 设置意图和快捷方式关联程序
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

        return shortcut;
    }

权限                                                                                   

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

调用                                                                                   

public void click1(View v) {
        Intent intent = this.getShortcutToDesktopIntent(MainActivity.this);
        sendBroadcast(intent);
    }

我是天王盖地虎的分割线                                                             

 

Android -- 创建桌面快捷方式,布布扣,bubuko.com

Android -- 创建桌面快捷方式

原文:http://www.cnblogs.com/yydcdut/p/3908615.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!