码迷,mamicode.com
首页 > 移动开发 > 详细

Android设置壁纸和创建桌面图标

时间:2014-12-20 16:59:06      阅读:424      评论:0      收藏:0      [点我收藏+]

标签:android   应用图标   桌面图标   创建图标   设置壁纸   

写了个小Demo,实现了设置壁纸和创建桌面图标的逻辑:

创建壁纸比较简单,将Drawable转为Bitmap,然后直接用setWallpaper就行了:

Bitmap bitmap = BitmapFactory.decodeResource(Main.this.getResources(), R.drawable.wallpaper);
                try {
                    Main.this.setWallpaper(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }

创建桌面图标:

if (!hasShortcut()) {
                    addShortcut();
                } else {
                    Toast.makeText(Main.this, "桌面图标已存在", Toast.LENGTH_SHORT).show();
                }


创建和删除桌面图标:
/**
     * 为程序创建桌面图标
     */
    private void addShortcut() {
        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); //不允许重复创建

        Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
        shortcutIntent.setClassName(this, this.getClass().getName());
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

        //图标
        Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
        sendBroadcast(shortcut);
    }

    /**
     * 删除应用的桌面图标
     */
    private void delShortcut() {
        Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

        //图标名称
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
        String appClass = this.getPackageName() + "." + this.getLocalClassName();
        ComponentName comp = new ComponentName(this.getPackageName(), appClass);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

        sendBroadcast(shortcut);

    }


判断桌面图标是否已经存在:

private boolean hasShortcut() {
        boolean isInstallShortcut = false;
        final ContentResolver cr = Main.this.getContentResolver();
        final String AUTHORITY = "com.android.launcher.settings";
        final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
        Cursor c = cr.query(CONTENT_URI, new String[]{"title", "iconResource"}, "title=?",
                new String[]{Main.this.getString(R.string.app_name).trim()}, null);
        if (c != null && c.getCount() > 0) {
            isInstallShortcut = true;
        }
        return isInstallShortcut;
    }

转载请注明出处:周木水的CSDN博客 http://blog.csdn.net/zhoumushui

我的GitHub:周木水的GitHub https://github.com/zhoumushui



Android设置壁纸和创建桌面图标

标签:android   应用图标   桌面图标   创建图标   设置壁纸   

原文地址:http://blog.csdn.net/zhoumushui/article/details/42042547

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!