7

我正在开发一个基于用户输入设置壁纸的简单应用程序。我缺少设置壁纸的代码。我一直在寻找它在很多网站都是徒劳的。任何人都可以发布一个示例代码,将壁纸设置为保存在res文件夹中的可绘制对象吗?

4

3 回答 3

5

适用于 Android 1.5 及更高版本

public void setWallpaper() {
  Context context = this.getBaseContext(); 
  Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), mImageIds[pos]);

  context.setWallpaper(mBitmap);
}
于 2010-08-04T15:54:17.613 回答
2

你可以试试

InputStream inputStream = getResources().openRawResource(wallpaperResource);
Bitmap setWallToDevice = BitmapFactory.decodeStream(inputStream);

try {
getApplicationContext().setWallpaper(setWallToDevice);
} catch (IOException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
}

但是这种方法已被弃用,因此您可以使用

try {
WallpaperManager.getInstance(getApplicationContext()).setResource(wallpaperResource);
} catch (IOException e){
e.printStackTrace();
}
于 2013-06-06T13:32:05.693 回答
0

在这里,我们如何从我们的 android 应用程序中设置墙纸

MainActivity.Java

public class AlarmActivity extends Activity{



       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);



            WallpaperManager myWallpaperManager
                    = WallpaperManager.getInstance(getApplicationContext());


                    try {
                        myWallpaperManager.setImageResource(R.raw.sample);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

    }
}

清单.xml

您应该提供此权限

<uses-permission android:name="android.permission.SET_WALLPAPER" />
于 2016-06-17T07:02:33.057 回答