Android 以编程方式设置安卓手机的背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20053919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:14:13  来源:igfitidea点击:

Programmatically set android phone's background

android

提问by Phate

I'd like to allow the user to choose a background from a list of images, the user clicks on one of them and that image is used as background for his phone. My app should simply be another version of the android default gallery.

我想允许用户从图像列表中选择一个背景,用户点击其中一个,该图像用作他手机的背景。我的应用程序应该只是 android 默认库的另一个版本。

Is it possible to programmatically set the phone's wallpaper?

是否可以以编程方式设置手机的壁纸?

回答by MrMins

First one, you need to set the permission in your Manifest.xmlfile

第一个,您需要在Manifest.xml文件中设置权限

 <uses-permission android:name="android.permission.SET_WALLPAPER"/>

And you can set the background with this:

你可以用这个设置背景:

Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.preview);
imagePreview.setImageResource(R.drawable.five);

buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            WallpaperManager myWallpaperManager 
            = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setResource(R.drawable.five);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
}});

回答by Lesleh

You can set a wallpaper using the WallpaperManager class. For example:

您可以使用WallpaperManager 类设置墙纸。例如:

WallpaperManager wallpaperManager =
        WallpaperManager.getInstance(getApplicationContext());
wallpaperManager.setBitmap(someBitmap);