以编程方式设置动态壁纸

发布于 2024-10-14 19:34:10 字数 54 浏览 6 评论 0原文

是否可以使用几行代码设置动态壁纸。例如,我想告诉我的用户可以使用动态壁纸“单击此处进行设置”。

Is it possible to set a live wallpaper using some lines of code. For example, i want to tell my users that a live wallpaper is available "click here to set it".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

記柔刀 2024-10-21 19:34:10

现在有两种方法可以实现这一点,因为 Jelly Bean 提供了直接设置动态壁纸的方法。此样板代码将选择可用的最佳方法。

Intent i = new Intent();

if(Build.VERSION.SDK_INT > 15){
    i.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);

    String p = HypercaneWallpaperService.class.getPackage().getName();
    String c = HypercaneWallpaperService.class.getCanonicalName();        
    i.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(p, c));
}
else{
    i.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
}
getActivity().startActivityForResult(i, 0);

There are now two ways to accomplish this as Jelly Bean provides a way to directly set the live wallpaper. This boilerplate code will choose the best method available.

Intent i = new Intent();

if(Build.VERSION.SDK_INT > 15){
    i.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);

    String p = HypercaneWallpaperService.class.getPackage().getName();
    String c = HypercaneWallpaperService.class.getCanonicalName();        
    i.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(p, c));
}
else{
    i.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
}
getActivity().startActivityForResult(i, 0);
聚集的泪 2024-10-21 19:34:10

好吧,这样我就不会因为过时的答案而遭到反对。请参阅下面的错误 454 的答案,以获得更强大的解决方案,该解决方案会将用户直接发送到 Jelly Bean 及更高版本设备上的壁纸预览屏幕。

==========================================

下面介绍如何启动壁纸选择器,从中用户可以选择您的壁纸。 Toast 只是向用户解释发生了什么的一种方式。

Toast toast = Toast.makeText(this, "Choose '<WALLPAPER NAME>' from the list to start the Live Wallpaper.",Toast.LENGTH_LONG);
toast.show();

Intent intent = new Intent();
intent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
startActivity(intent);

Alright, just so I stop getting downvotes for an outdated answer. Please see Error 454's answer below for a more robust solution which will send the user directly to the wallpaper preview screen on Jelly Bean and up devices.

========================================

Here's how to start the wallpaper chooser, from which the user can select your wallpaper. The toast is just a way to explain to the user what's going on.

Toast toast = Toast.makeText(this, "Choose '<WALLPAPER NAME>' from the list to start the Live Wallpaper.",Toast.LENGTH_LONG);
toast.show();

Intent intent = new Intent();
intent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
startActivity(intent);
静谧 2024-10-21 19:34:10

您应该使用 async_wallpaper 包来执行此操作。您可以通过 URL 轻松访问 mp4 文件并在您的应用程序中显示它们,也可以将它们应用到主屏幕上

You should use the async_wallpaper package to perform this operation. You can easily access the mp4 files through URLs and show them in your app and also apply them on the home screen

滴情不沾 2024-10-21 19:34:10

您必须让壁纸管理器来设置壁纸。这是我的一个应用程序中使用的代码,它使用 AsyncTask(已弃用),但您也可以使用 Kotlin 协程来实现。

class SetWallpaperTask : AsyncTask<String?, Void?, Bitmap?>() {
    override fun doInBackground(vararg params: String?): Bitmap? {
        val wallpaperManager = WallpaperManager.getInstance(applicationContext)
        var result: Bitmap? = null
        try {
            result =
                Glide.with(this@WallpaperPreviewActivity).asBitmap().load(fullhdurl).submit()
                    .get()
        } catch (e: ExecutionException) {
            e.printStackTrace()
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }


        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                wallpaperManager.setBitmap(result)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return result
    }

    override fun onPostExecute(result: Bitmap?) {
        super.onPostExecute(result)

        Toast.makeText(this@WallpaperPreviewActivity, "Wallpaper Applied!", Toast.LENGTH_SHORT)
            .show()
    }

    override fun onPreExecute() {
        super.onPreExecute()
        // show progress bar or dialog
    }
}

You have to get wallpaper manager to set wallpaper. Here is the code that is used in one of my apps, It uses AsyncTask (which is deprecated) but you can implement with Kotlin coroutines as well.

class SetWallpaperTask : AsyncTask<String?, Void?, Bitmap?>() {
    override fun doInBackground(vararg params: String?): Bitmap? {
        val wallpaperManager = WallpaperManager.getInstance(applicationContext)
        var result: Bitmap? = null
        try {
            result =
                Glide.with(this@WallpaperPreviewActivity).asBitmap().load(fullhdurl).submit()
                    .get()
        } catch (e: ExecutionException) {
            e.printStackTrace()
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }


        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                wallpaperManager.setBitmap(result)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return result
    }

    override fun onPostExecute(result: Bitmap?) {
        super.onPostExecute(result)

        Toast.makeText(this@WallpaperPreviewActivity, "Wallpaper Applied!", Toast.LENGTH_SHORT)
            .show()
    }

    override fun onPreExecute() {
        super.onPreExecute()
        // show progress bar or dialog
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文