Android sets wallpaper and creates desktop icons, android Wallpaper

Source: Internet
Author: User

Android sets wallpaper and creates desktop icons, android Wallpaper

I wrote a Demo to implement the logic of setting wallpaper and creating desktop icons:

Creating a wallpaper is relatively simple. Convert Drawable into Bitmap and use setWallpaper directly:

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

Create a desktop icon:

If (! HasShortcut () {addShortcut ();} else {Toast. makeText (Main. this, "the desktop icon already exists", Toast. LENGTH_SHORT). show ();}


Create and delete a desktop icon:
/*** Create a desktop icon for the Program */private void addShortcut () {Intent shortcut = new Intent ("com. android. launcher. action. INSTALL_SHORTCUT "); shortcut. putExtra (Intent. EXTRA_SHORTCUT_NAME, getString (R. string. app_name); // The shortcut name shortcut. putExtra ("duplicate", false); // repeated Intent shortcutIntent = new Intent (Intent. ACTION_MAIN); shortcutIntent. setClassName (this, this. getClass (). getName (); shortcut cut. putExtra (Intent. EXTRA_SHORTCUT_INTENT, shortcutIntent); // icon Intent. export cuticonresource iconRes = Intent. using cuticonresource. fromContext (this, R. drawable. ic_launcher); shortcut cut. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); sendBroadcast (Distributed cut);}/*** Delete the desktop icon of an application */private void delShortcut () {Intent distributed cut = new Intent ("com. android. launcher. action. UNINSTALL_SHORTCUT "); // The icon name is 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 );}


Determine whether 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;    }

Reprinted please indicate the source: Zhou mu Shui CSDN blog http://blog.csdn.net/zhoumushui

My GitHub: Zhou mu Shui's GitHub https://github.com/zhoumushui



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.