Несколько запутался как прописать сохранение данных в приложении. Необходимо сохранять id случайного изображения (randomImageId) до конца суток (это как-то можно прописать без дополнительного метода по очистке данных?)
public class Activity extends AppCompatActivity {
public static final String APP_PREFERENCES = "mysettings";
public static final String APP_PREFERENCES_INDEX = "index";
SharedPreferences mSettings;
@Override
public void onBackPressed() {
final TextView infotext = findViewById(R.id.infotext);
ImageButton resetbutton = findViewById(R.id.resetbutton);
infotext.setVisibility(View.INVISIBLE);
resetbutton.setVisibility(View.VISIBLE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity);
mSettings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mSettings.edit();
editor //some code
editor.apply();
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
final Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
final ImageButton resetbutton = findViewById(R.id.resetbutton);
final ImageButton cardbutton = findViewById(R.id.cardbutton);
cardbutton.setTag(1);
final TextView infotext = findViewById(R.id.infotext);
final int[] imageIds = {
R.drawable.n00,
R.drawable.n01,
R.drawable.n02,
R.drawable.n03,
R.drawable.n04,
R.drawable.n05,
R.drawable.n06,
R.drawable.n07,
R.drawable.n08,
R.drawable.n09,
R.drawable.n10,
R.drawable.n11,
R.drawable.n12,
R.drawable.n13,
R.drawable.n14,
R.drawable.n15,
R.drawable.n16,
R.drawable.n17,
R.drawable.n18,
R.drawable.n19,
R.drawable.n20,
R.drawable.n21,
R.drawable.n22,
};
final String[] texts = {
getString(R.string.s00),
"Mage",
"Pristess",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22"
};
cardbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
infotext.setMovementMethod(new ScrollingMovementMethod());
int status = (Integer) v.getTag();
if (status == 1) {
Random generator = new Random();
int index = generator.nextInt(imageIds.length);
int randomImageId = imageIds [index];
String text = texts[index];
cardbutton.setImageResource(randomImageId);
infotext.setText(text);
infotext.setBackgroundColor(Color.parseColor("#ff5013"));
infotext.setVisibility(View.INVISIBLE);
resetbutton.setVisibility(View.INVISIBLE);
v.setTag(0);
vibrator.vibrate(50);
} else {
infotext.setVisibility(View.VISIBLE);
resetbutton.setVisibility(View.INVISIBLE);
v.setTag(0);
}
}
});
resetbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(i);
}
});
}