重启设备后如何使用bootreciever设置壁纸? (android,eclipse)

时间:2014-09-09 00:36:35

标签: java android eclipse

我正在制作一个壁纸应用程序,但我有一个问题,重新启动手机后壁纸放大,所以我尝试使用bootreciever,它做了我想要它做但但它只适用于第一个图像在应用程序,每当我将任何其他图像设置为壁纸并重新启动设备时,图像将更改为第一张图像。

所以有人知道如何解决这个问题,提前谢谢,

这是我的MainActivity java:

公共类MainActivity扩展了Activity实现OnClickListener {

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



}

}

这是我的BootReceiver java:

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

New BootReciever:

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

 @Override public void onReceive(Context context,Intent intent){
 Intent in = new Intent(context, MainActivity.class);
 in.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    context.startActivity(in);
try{ 
    SharedPreferences       sharedPreferences = context.getSharedPreferences("wallpaperapp",MainActivity.tophone);
        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.someDefault);
                    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());
}
}
}

1 个答案:

答案 0 :(得分:1)

首先改变

SharedPreferences       sharedPreferences = getSharedPreferences("wallpaperapp",0);

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);

每当您更改壁纸时,都应该在onClick方法中将新更改的壁纸的资源ID保存到共享首选项文件中:

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

注意* putInt方法中的KEY变量是一个常量,您必须定义它,它是您想要更改的首选项的名称,您可以为其赋予值" wallpaperapp"

现在,您只需在手机完成启动后检索ID即可。使用相同的KEY

int retrievedImageID = sharedPreference.getInt(KEY, someDefaultValue);

然后只需设置图像

display.setImageResource(retrievedImageID);

这是关于sharedPreferences的android官方文档 http://developer.android.com/training/basics/data-storage/shared-preferences.html

你的MainActivity看起来像是:

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

  protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);
    //add these lines
    sharedPreference = getPreferences(MODE_PRIVATE);
    changeWallpaper();
  }

public void onClick(View v) {
   switch(){}
   // add this line below the switch statement
   sharedPreference.edit().putInt(KEY, v.getId()).commit();
}
//add this method
public void changeWallpaper(){
   int someDefaultValue = R.id.iv1;
   retrievedImageID = sharedPreference.getInt(KEY, someDefaultValue);
   display.setImageResource(retrievedImageID);
}

你的启动接收器:

public void onReceive(Context context,Intent intent){
    // comment out the existing code in here
    // and add the following lines
    Intent in = new Intent(context, MainActivity.class);
    // these flags must be set for the receiver to start the activity
    in.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    context.startActivity(in);
}