I searched on how you could manipulate the current wallpaper of the android mobile and here it is the code:
AndroidManifest.xml
<uses-permission name="android.permission.SET_WALLPAPER">
main.xml
<?xml version="1.0" encoding="utf-8"?>
<framelayout android="http://schemas.android.com/apk/res/android" layout_width="wrap_content" layout_height="wrap_content">
<imageview layout_width="wrap_content" layout_height="wrap_content" id="@+id/imageview">
<linearlayout orientation="horizontal" layout_width="wrap_content" layout_height="fill_parent">
<button id="@+id/randomize" layout_width="wrap_content" layout_height="wrap_content" text="@string/randomize" layout_gravity="bottom">
</button><button id="@+id/setwallpaper" layout_width="wrap_content" layout_height="wrap_content" text="@string/set_wallpaper" layout_gravity="bottom">
SetWallpaperActivity.java
package com.kites.sarath.wallpaperdemo;
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import android.app.WallpaperManager;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class SetWallpaperActivity extends Activity {
final static private int[] mColors =
{Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN,
Color.YELLOW, Color.WHITE};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
final ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setDrawingCacheEnabled(true);
imageView.setImageDrawable(wallpaperDrawable);
Button randomize = (Button) findViewById(R.id.randomize);
randomize.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
int mColor = (int) Math.floor(Math.random() * mColors.length);
wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY);
imageView.setImageDrawable(wallpaperDrawable);
imageView.invalidate();
}
});
Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
setWallpaper.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
try {
wallpaperManager.setBitmap(imageView.getDrawingCache());
finish();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
Nice work !!! Keep it up....share your knowledge to students and programmers....
ReplyDeleteGood job.
ReplyDeleteit would be nice,if you explain the code for sending commands to server from an android App here.