Search code examples
androidkotlinlockscreenwallpaper

How to set lock screen wallpaper on devices prior to API 24 in Android?


I have an image and want to set it as the lock screen wallpaper. For devices with API 24 and higher, we can do this with

wallpaperManager.setBitmap(bitmap,null,true,WallpaperManager.FLAG_LOCK)

But I want to do it on devices prior to API 24 as many other wallpaper apps do so. This has been answered here before, but it doesn't give any clue how other wallpaper applications are able to set the lock screen wallpaper on devices prior to API 24. There are other solutions that suggest that the app has to be registered as a media controller for a temporary replacement, but that's not my case. Note that I am aware of the fact that it is not possible through the standard API, however, I am looking for an alternative.


Solution

  • Use this Implicit intent to set the wallpaper or set lock screen wallpaper on devices prior to API 24.

    Intent intent = new Intent("android.intent.action.ATTACH_DATA");
    intent.addCategory("android.intent.category.DEFAULT");
    String str = "image/*";
    intent.setDataAndType(Uri.fromFile(new File(your_file_url)), str);
    intent.putExtra("mimeType", str);
    startActivity(Intent.createChooser(intent, "Set As:"));