Here the sample code for wallpaper
public class WallpaperMainActivity extends Activity {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@SuppressWarnings("deprecation")
private final GestureDetector detector = new GestureDetector(new SwipeGestureDetector());
/** A list containing the resource identifiers for all of our selectable backgrounds */
private static final List<Integer> backgrounds = new ArrayList<Integer>();
/** The total number of backgrounds in the list */
private static final int TOTAL_IMAGES;
/** Instantiate the list statically, so this will be done once on app load, also
calculate the total number of backgrounds */
static {
backgrounds.add(R.drawable.background1);
backgrounds.add(R.drawable.background2);
backgrounds.add(R.drawable.background3);
backgrounds.add(R.drawable.background4);
backgrounds.add(R.drawable.background5);
backgrounds.add(R.drawable.background6);
backgrounds.add(R.drawable.background7);
backgrounds.add(R.drawable.background8);
backgrounds.add(R.drawable.background9);
backgrounds.add(R.drawable.background10);
// We -1 as lists are zero indexed (0-2 is a size of 3) - we'll mke use of this to
implement a browsing loop
TOTAL_IMAGES = (backgrounds.size() - 1);
}
/** the state of what wallpaper is currently being previewed */
private int currentPosition = 0;
/** our image wallpaper preview */
private ImageView backgroundPreview;
/** A helper class that will do the heavy work of decoding images and actually setting
the wallpaper */
private HeavyLifter chuckNorris;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wallpaper_main);
backgroundPreview = (ImageView) findViewById(R.id.backgroundPreview);
backgroundPreview.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
// Set the default image to be shown to start with
changePreviewImage(currentPosition);
// Load are heavy lifter (goes and does work on another thread), to get a response
after the lifters thread
// has finished we pass in a Handler that will be notified when it completes
chuckNorris = new HeavyLifter(this, chuckFinishedHandler);
}
/**
* Called from XML when the previous button is pressed
* Decrement the current state position
* If the position is now less than 0 loop round and show the last image (the array size)
* @param v
*/
public void gotoPreviousImage(View v) {
int positionToMoveTo = currentPosition;
positionToMoveTo--;
if(positionToMoveTo < 0){
positionToMoveTo = TOTAL_IMAGES;
}
changePreviewImage(positionToMoveTo);
}
/**
* Called from XML when the set wallpaper button is pressed
* Thie retrieves the id of the current image from our list
* It then asks chuck to set it as a wallpaper!
* The chuckHandler will be called when this operation is complete
* @param v
*/
public void setAsWallpaper(View v) {
int resourceId = backgrounds.get(currentPosition);
chuckNorris.setResourceAsWallpaper(resourceId);
AlertDialog.Builder ab=new AlertDialog.Builder(WallpaperMainActivity.this);
ab.setIcon(R.drawable.ic_launcheralert);
ab.setTitle("Image set as Background....");
ab.setNeutralButton("Ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
} );
ab.show();
}
/**
* Called from XML when the next button is pressed
* Increment the current state position
* If the position is now greater than are array size loop round and show the first image again
* @param v
*/
public void gotoNextImage(View v) {
int positionToMoveTo = currentPosition;
positionToMoveTo++;
if(currentPosition == TOTAL_IMAGES){
positionToMoveTo = 0;
}
changePreviewImage(positionToMoveTo);
}
/**
* Change the currently showing image on the screen
* This is quite an expensive operation as each time the system
* has to decode the image from our resources - alternatives are possible (a list of drawables created at app start)
* @param pos the position in {@link MainActivity#backgrounds} to select the image from
*/
public void changePreviewImage(int pos) {
currentPosition = pos;
backgroundPreview.setImageResource(backgrounds.get(pos));
Log.d("Main", "Current position: "+pos);
}
/**
* This is the handler that is notified when are HeavyLifter is finished doing an operation
*/
private Handler chuckFinishedHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case SUCCESS:
break;
case FAIL:
Toast.makeText(WallpaperMainActivity.this, "Uh oh, can't do that right now", Toast.LENGTH_SHORT).show();
break;
default:
super.handleMessage(msg);
}
}
};
class SwipeGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
int positionToMoveTo = currentPosition;
positionToMoveTo--;
if(positionToMoveTo < 0){
positionToMoveTo = TOTAL_IMAGES;
}
changePreviewImage(positionToMoveTo);
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// left to right swipe
int positionToMoveTo = currentPosition;
positionToMoveTo++;
if(currentPosition == TOTAL_IMAGES){
positionToMoveTo = 0;
}
changePreviewImage(positionToMoveTo);
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
}
Create Another new class
public class HeavyLifter {
public static final int SUCCESS = 0;
public static final int FAIL = 1;
private final Context context;
private final Handler callback;
private WallpaperManager manager;
/**
* Setup the HeavyLifter
* @param context the context we are running in - typically an activity
* @param callback the handler you want to be notified when we finish doing an operation
*/
public HeavyLifter(Context context, Handler callback) {
this.context = context;
this.callback = callback;
this.manager = (WallpaperManager)
context.getSystemService(Context.WALLPAPER_SERVICE);
}
/**
* Takes a resource id of a file within our /res/drawable/ folder<br/>
* It then spawns a new thread to do its work on<br/>
* The reource is decoded and converted to a byte array<br/>
* This array is passed to the system which can use it to set the phones wallpaper<br/>
* Upon completion the callback handler is sent a message with eith {@link
HeavyLifter#SUCCESS} or {@link HeavyLifter#FAIL}
*
* @param resourceId id of a file within our /res/drawable/ folder
*/
public void setResourceAsWallpaper(final int resourceId) {
new Thread() {
@Override
public void run() {
try {
manager.setBitmap(getImage(resourceId));
callback.sendEmptyMessage(SUCCESS);
} catch (IOException e) {
Log.e("Main", "Cant set wallpaper");
callback.sendEmptyMessage(FAIL);
}
}
}.start();
}
/**
* Decodes a resource into a bitmap, here it uses the convenience method
'BitmapFactory.decodeResource', but you can decode
* using alternatives these will give you more control over the size and quality of the
resource.
* You may need certain size resources within each of your /hdpi/ /mdpi/ /ldpi/ folders
in order
* to have the quality and look you want on each different phone.
*/
private Bitmap getImage(int resourceId) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId,
null);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,
manager.getDesiredMinimumWidth(), manager.getDesiredMinimumHeight(), true);
bitmap.recycle();
bitmap = null;
return scaledBitmap;
}
}
add this to your manifest
<uses-permission android:name="android.permission.SET_WALLPAPER" />
and
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>