Android : How to use Broadcast Receiver to make walpaper fit the screen?

on Tuesday, September 16, 2014


I am making a wallpaper app, you have a bunch of photos and the you select becomes your phone wallpaper and everything works good except when I restart my phone the wallpaper zooms in, and I can't fix it because I can't make it select the photo that the user choose all I could do is make it work only for the first pic but I can't do it for the others can someone tell me what should I do.


here is what I tried: my MainActivity:



static int tophone;
ImageView display;
public static Integer[] tophone2 = {
R.drawable.iv1,R.drawable.iv2,R.drawable.iv3,R.drawable.iv4 };


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

tophone = R.drawable.iv1;
display = (ImageView) findViewById(R.id.IView);
ImageView image1 = (ImageView) findViewById(R.id.iv1);
ImageView image2 = (ImageView) findViewById(R.id.iv2);
ImageView image3 = (ImageView) findViewById(R.id.iv3);
ImageView image4 = (ImageView) findViewById(R.id.iv4);
Button setWp = (Button) findViewById(R.id.setWp);

Picasso.with(MainActivity.this).load(R.drawable.iv1_s).into(image1);
Picasso.with(MainActivity.this).load(R.drawable.iv2_s).into(image2);
Picasso.with(MainActivity.this).load(R.drawable.iv3_s).into(image3);
Picasso.with(MainActivity.this).load(R.drawable.iv4_s).into(image4);


image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
setWp.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.iv1:
display.setImageResource(R.drawable.iv1);
tophone = tophone2[0];
break;
case R.id.iv2:
display.setImageResource(R.drawable.iv2);
tophone = tophone2[1];
break;
case R.id.iv3:
display.setImageResource(R.drawable.iv3);
tophone = tophone2[2];
break;
case R.id.iv4:
display.setImageResource(R.drawable.iv4);
tophone = tophone2[3];
break;
case R.id.setWp:
Toast WpSet = Toast.makeText(MainActivity.this,"Wallpaper Set", Toast.LENGTH_SHORT);

SharedPreferences sharedPreferences = getSharedPreferences("wallpaperapp",0);
sharedPreferences.edit().putInt("position",0).commit();

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), tophone);
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
WpSet.show();
break;
}



}

}


and my Broadcast Receiver:



public class BootReceiver extends BroadcastReceiver {
private static final String TAG="BootReceiver";

@Override public void onReceive(Context context,Intent intent){
try{
SharedPreferences sharedPreferences = context.getSharedPreferences("wallpaperapp",0);
int position= sharedPreferences.getInt("position", 0);
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap tempbitMap = BitmapFactory.decodeResource(context.getResources(),MainActivity.tophone2[position]);
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception e){
Log.e(TAG,e.toString());
}
}
}

0 comments:

Post a Comment