如何使用Sharedpreference存储int?

时间:2014-09-18 20:43:34

标签: java android eclipse sharedpreferences

我尝试使用sharedpreference来存储一个int,int根据用户选择的内容更改它的id,我不知道如何存储用户选择的那个以及如何读取它,我现在主要关心的是如何存储我将在稍后阅读它如何阅读,但请有人告诉我如何解决这个问题。

这是我的MainActivity:

  public class MainActivity extends Activity implements OnClickListener {

static int tophone;
ImageView display;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    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);

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

正如您所看到的,我想要存储图像的ID,即#34; tophone"。 我想存储id,所以我可以在广播记录中引用它。 提前致谢

1 个答案:

答案 0 :(得分:4)

小例子:

集:

SharedPreferences sp = getSharedPreferences("myPrefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", yourIntValue);
editor.commit();

得到:

 SharedPreferences sp = getSharedPreferences("myPrefs", Activity.MODE_PRIVATE);
 int myIntValue = sp.getInt("your_int_key", 0);

SharedPrefs中的BroadReceiver获取int值:

您可以使用onReceive(Context context, Intent arg1)的{​​{1}}内容。

BroadReceiver