Wrote a small demo that implements the logic of setting up wallpapers and creating desktop icons:
Creating a wallpaper is simpler, turning drawable into bitmap, and then directly using SetWallpaper on the line:
Bitmap Bitmap = Bitmapfactory.decoderesource (Main.this.getResources (), r.drawable.wallpaper); try { Main.this.setWallpaper (bitmap); } catch (IOException e) { e.printstacktrace (); }
To create a desktop icon:
if (!hasshortcut ()) { addshortcut (); } else { toast.maketext (main.this, "desktop icon already exists", toast.length_short). Show (); }
To create and delete desktop icons:
/** * Create desktop icons for programs */private void Addshortcut () {Intent shortcut = new Intent ("Com.android.launcher.actio N.install_shortcut "); Shortcut.putextra (Intent.extra_shortcut_name, getString (r.string.app_name));//The name of the shortcut Shortcut.putextra (" Duplicate ", false); Duplicate creation of Intent shortcutintent = new Intent (intent.action_main) is not allowed; Shortcutintent.setclassname (this, This.getclass (). GetName ()); Shortcut.putextra (Intent.extra_shortcut_intent, shortcutintent); Icon Intent.shortcuticonresource iconres = Intent.ShortcutIconResource.fromContext (this, r.drawable.ic_launcher); Shortcut.putextra (Intent.extra_shortcut_icon_resource, iconres); Sendbroadcast (shortcut); }/** * Delete the app's desktop icon */private void Delshortcut () {Intent shortcut = new Intent ("Com.android.launcher. Action. Uninstall_shortcut "); Icon Name Shortcut.putextra (Intent.extra_shortcut_name, getString (r.string.app_name)); String APPCLThis.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); }
Determine if the desktop icon already exists:
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; }
Reprint Please specify source: Zhou Mushi's csdn blog Http://blog.csdn.net/zhoumushui
My github: Zhou Mushi's GitHub Https://github.com/zhoumushui
Android Settings wallpapers and create desktop icons