重启Android后如何确保用户选择的图片无法更改?

时间:2015-11-02 04:13:57

标签: java android sharedpreferences

我有一个简单的贴纸小部件,允许用户在放置小部件时选择其中一个贴纸。我的问题是,一旦设备重新启动,贴纸将恢复为默认设置。无论设备是否重新启动,如何确保用户选择的标签仍然存在?一个例子或推动正确的方向将是非常有帮助的。我不是在寻找任何人为我做这件事,只是为了接受如何去做这件事的教育。谢谢。

修改 包含的代码

我不确定,但我认为我这一切都错了。我不知道如何以我现在拥有的方式合并SharedPreferences。有人能给我一个例子说明我应该如何使用至少一张我正在使用的图片吗?

public class SpraycanConfig extends Activity
{
int xawID;
AppWidgetManager xawm;
Context xc;
EditText xinfo;
//EasyTracker easyTracker;

protected void onCreate(Bundle paramBundle)
{
 super.onCreate(paramBundle);

 Crashlytics.start(this);

setContentView(R.layout.scconfiglayout);



  this.xc = this;
  Bundle localBundle = getIntent().getExtras();
  if (localBundle != null) {
  this.xawID = localBundle.getInt("appWidgetId", 0);
  }
  for (;;)
  {
    this.xawm = AppWidgetManager.getInstance(this.xc);
    ImageButton localImageButton1 = (ImageButton)findViewById(R.id.normal);
View.OnClickListener local1 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.normal);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();
  }
};
localImageButton1.setOnClickListener(local1);
ImageButton localImageButton2 = (ImageButton)findViewById(R.id.belton);
View.OnClickListener local2 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.belton);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton2.setOnClickListener(local2);
ImageButton localImageButton3 = (ImageButton)findViewById(R.id.gold);
View.OnClickListener local3 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.gold);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton3.setOnClickListener(local3);
ImageButton localImageButton4 = (ImageButton)findViewById(R.id.hardcore);
View.OnClickListener local4 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.hardcore);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton4.setOnClickListener(local4);
ImageButton localImageButton5 = (ImageButton)findViewById(R.id.ironlak);
View.OnClickListener local5 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.ironlak);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton5.setOnClickListener(local5);
ImageButton localImageButton6 = (ImageButton)findViewById(R.id.krylon);
View.OnClickListener local6 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.krylon);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton6.setOnClickListener(local6);
ImageButton localImageButton7 = (ImageButton)findViewById(R.id.mtn94);
View.OnClickListener local7 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.ninetyfour);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton7.setOnClickListener(local7);
ImageButton localImageButton8 = (ImageButton)findViewById(R.id.rusto);
View.OnClickListener local8 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.rusto);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton8.setOnClickListener(local8);

return;

  }

 }

@Override
protected void onStart() {
    super.onStart();

}

@Override
protected void onStop() {
    super.onStop();

 }



}

4 个答案:

答案 0 :(得分:3)

SharedPreferences将是您的最佳选择。

实际上,您可以使用android中使用的任何存储方式保存所选图像。

答案 1 :(得分:2)

我最喜欢的方法是使用SharedPreferences

这就是你要做的事情:

1)初始化SharedPreferences对象。如果您想存储影响应用程序不同方面的值,请使用标记进行分类。

        $( '.fo-content li' ).on( 'click', function(a){
            $this_li = this; // ADD THIS LINE
            a.preventDefault();

            if( $( this ).css( "height" ) == "270px" ) {
                inner.removeClass("opened");
                list.animate({ height: "270px" }, 500 );
                $(this).animate({height: "450px" }, 
                { 
                duration: 500, 
                complete: function() {
                    $("html, body").animate({ scrollTop: $(this).offset().top },
                        {
                            duration:  500,
                            complete: function() {
                                $($this_li).find(".inner").addClass("opened"); // CHANGE THIS LINE
                            }
                        });
                    }
                });
            }
            else {
                $(this).animate({ height: "270px" }, 500 );
                inner.removeClass("opened");
            }           
        });

2)检索与其关联的值。在您的情况下,它可以是URL或资产ID。该函数需要第二个值,如果尚未设置首选项,则返回该值。

SharedPreferences myPrefs = getSharedPreferences("Stickers", MODE_PRIVATE);

3)存储新值或替换现有值。

myPrefs.getString("SelectedSticker", "default.jpg");

希望这有帮助!

答案 2 :(得分:2)

完全相同:

$(this).ajaxSubmit({
      url: "{{path('check_login_ajax')}}",
      type: "POST",
      success: function (result){

      }
)};

答案 3 :(得分:0)

坚持你的形象或你可以去了解的逻辑,用户是否真的改变了。

关于将共享偏好放在哪里的问题,把它放在改变Image的代码函数或你所说的内容中,你可以使用I / O写入临时存储,让我们说Context.getCacheDir()你可以将图像保留在那里,然后在你的onResume()中检查该文件是否与File.exist()

一起存在

还要记得在完成访问后删除它。