如何设置壁纸图片permently? (android,eclipse)

时间:2014-09-09 22:15:46

标签: java android eclipse

我正在制作一个Android壁纸应用程序,但每当我重新启动手机时壁纸放大,所以我尝试使用BootReciever,但我没有发生任何不同的事情,重新启动手机后壁纸仍然会变焦。

所以这是我的MainActivity java:

public class MainActivity extends Activity implements OnClickListener {
public static final String KEY = "wallpaperapp";
          private SharedPreferences sharedPreference;
          private int retrievedImageID;

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);

    sharedPreference = getPreferences(MODE_PRIVATE);
    changeWallpaper();
}

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

        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;

}
    sharedPreference.edit().putInt(KEY, v.getId()).commit();



}
public void changeWallpaper(){
       int someDefaultValue = R.id.iv1;
       retrievedImageID = sharedPreference.getInt(KEY, someDefaultValue);
       display.setImageResource(retrievedImageID);
}
}

这是我的BootReciever java:

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

@Override public void onReceive(Context context,Intent intent){
try{
        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.tophone);
        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 个答案:

没有答案