标签: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 应用图标 桌面图标 创建图标 设置壁纸
原文地址:http://blog.csdn.net/zhoumushui/article/details/42042547