Set wallpaper and create desktop icons for Android
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 the 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; }