如何确保用户select的图像重新启动Android后不会改变?

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

编辑 包括代码

我不确定,但我认为我是这样做的。 我不知道如何以现在的方式整合SharedPreferences。 有人可以给我一个例子,我应该如何去做这至less与我使用的一个图像?

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

Solutions Collecting From Web of "如何确保用户select的图像重新启动Android后不会改变?"

完全如此:

 protected void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); SharedPreferences sharedPreferences = getSharedPreferences("myPref", 0); // get the last chosen sticker int sticker = sharedPreferences.getInt("chosenSticker", R.drawable.normal); //the .normal is the default value in case the user didn't choose one. myView.setImageViewResource(R.id.imageview, sticker); //now you can use it // store user's choice for example: View.OnClickListener local2 = new View.OnClickListener() { public void onClick(View paramAnonymousView) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("chosenSticker", R.drawable.belton); editor.apply(); // your rest of your code here ... } } } 

SharedPreferences将是您的最佳select。

其实你可以保存在Android中使用的任何存储方式选定的图像。

我最喜欢的方式是使用SharedPreferences 。

这是你如何去做的:

1)初始化一个SharedPreferences对象。 使用标签进行分类,以防止存储影响应用程序不同方面的值。

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

2)检索与之相关的值。 在你的情况下,它可能是一个URL或资产ID。 该函数需要第二个值,如果首选项尚未设置,将返回该值。

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

3)存储新的值或replace现有的值。

 public void setStickerPreference(String stickerURL) { // retrieve editor SharedPreferences.Editor myPrefsEditor = myPrefs.edit(); // save or modify the field myPrefsEditor.putString("SelectedSticker", stickerURL); // commit changes prefsEditor.commit(); } 

希望这可以帮助!

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

你的问题放在哪里把sharedpreference,把它放在代码函数,改变图像或你在说什么,你可以使用I / O写入临时存储让我们说Context.getCacheDir(),你可以坚持你的形象在那里,然后在你的onResume()还检查该文件是否存在与File.exist()

另外,请记住在完成访问时删除它。