How do I refresh the wallpaper image without reboot after restarting the Android SystemUIService?

前端 未结 1 1363
清歌不尽
清歌不尽 2021-01-15 00:39

I\'m creating an enterprise app for my company that will only run on a company provided 7\" tablet which is rooted and running Android 4.2.2. A requirement is that when the

相关标签:
1条回答
  • 2021-01-15 01:04

    After further investigation i discovered that when i run these commands, the wallpaper only disappears when it was an image and not a live wallpaper or video wallpaper.

    I realise that this is quite specific to an uncommon scenario, but I thought I would share my solution in the event that it may help somebody else in the future. I hoped that there was a command that i could run that would resolve this but couldn't find anything so I decided to refresh the wallpaper programmatically. WallpaperManager doesn't have a refresh method at this stage so I just get the existing wallpaper image and then set the wallpaper to that image. A bit of a hack but in my opinion it is better than leaving the user with a black wallpaper.

    private void refreshWallpaper() {
        try {
            WallpaperManager wallpaperManager = 
                WallpaperManager.getInstance(context);
            WallpaperInfo wallpaperInfo = wallpaperManager.getWallpaperInfo();
    
            // wallpaperInfo is null if a static wallpaper is selected and
            // is not null for live wallpaper & video wallpaper.
            if (wallpaperInfo == null) {
                // get the existing wallpaper drawable
                Drawable wallpaper = wallpaperManager.peekDrawable();
    
                // convert it to a bitmap
                Bitmap wallpaperBitmap = drawableToBitmap(wallpaper);
    
                // reset the bitmap to the current wallpaper
                wallpaperManager.setBitmap(wallpaperBitmap);    
            }
        } catch (Exception e) {
            // TODO: Handle exception as needed
            e.printStackTrace();
        }
    }
    
    private static Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) { 
            return ((BitmapDrawable) drawable).getBitmap(); 
        }
    
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
            drawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    
        return bitmap;
    }
    
    0 讨论(0)
提交回复
热议问题