In this post, you would learn how to Set Remote Image as Wallpaper Asynchronously.
Important Key Notes:
- To use WallpaperManager, minimum API level have to be set to 5.
- Add the relevant permissions to your AndroidManifest.xml file:
<
uses-permission
android:name
=
"android.permission.SET_WALLPAPER"
>
- A different thread should be used, because setting wallpaper is a time consuming task. Otherwise you may get ANR error. Here, I have used Async Task.
private class SetWallpaperAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String URL = "path/of/remote/image";
setWallpaper(URL);
return "Executed";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
private void setWallpaper(String url) {
try {
WallpaperManager wpm = WallpaperManager.getInstance(ActivitySinglePage.this);
InputStream ins = new URL(url).openStream();
wpm.setStream(ins);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Then call from where you need
new SetWallpaperAsyncTask().execute("");
No comments:
Post a Comment