Android Settings wallpapers and create desktop icons

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.