To set or get the wallpaper of android phone from your application, we used WallpaperManager. This class provides access to the system wallpaper. By the help of this class you can get the current wallpaper of your phone inside your app and even can change the current wallpaper from your application. Also you can get the desired dimensions for the wallpaper.
For more details about this class visit link
First you need to create the instance of WallpaperManager and Handler
private WallpaperManager wallpaperManager;
private Handler handler;
Now, inside onCreate()
// instantiating wallpaperManager and handler
wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
handler = new Handler();
Method to set the wallpaper
// set Wallpaper method
private void setWallPaper()
{
Thread thread = new Thread() {
public void run() {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
Drawable drawable = getResources().getDrawable(R.drawable.mclaren);
try {
wallpaperManager.setBitmap("place your bitmap obj here");
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "WallPaper Changed successfully", Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "WallPaper not changed", Toast.LENGTH_SHORT).show();
}
});
}
}
};
thread.start();
}
To get the wallpaper
mainImage.setImageDrawable(wallpaperManager.getDrawable());
Note:- mainImage is the imagview in layout.
Use the following permission in your manifest file
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
0 Comment(s)