Home > OS >  Image view is not changing periodically
Image view is not changing periodically

Time:11-11

I need a Image view in my android app to change on the daily, at the moment I am using a onRecieve class that runs every 24 hours (utilizing alarm manager). This triggers a random val (image) to be selected and then for the image in the activity to be set to that image previously selected (using setImageResource). It doesn't work however and the image view stays blank with no image set.

Here is the receiver code -

class MyReceiver : BroadcastReceiver() {


override fun onReceive(context: Context, intent: Intent) {


        Toast.makeText(context,"This toast will be shown every X minutes", Toast.LENGTH_LONG).show()


        if (context !is Qinperation) return

        val imageView = context.findViewById<View>(R.id.paininass) as ImageView
        val quotes = arrayOf(R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i5, R.drawable.i6, R.drawable.i7, R.drawable.i8, R.drawable.i9, R.drawable.i10, R.drawable.i11, R.drawable.i12)
        val quote = quotes.random()
        imageView.setImageResource(quote)


}
}

Any ideas or suggestions? I don't know what's going wrong as there are no errors, it just doesn't work.

CodePudding user response:

findViewById works on Views (and Activities), you can't just call it on a Context - they don't have view hierarchies. And even if you could, setting an image on some ImageView would only affect that object for as long as it's around. If you open the app and another ImageView is inflated, it will have whatever image was set in the XML or whatever, the default you've specified.

If you want to "remember" things like that, you need to store that data, and then read it when you're setting up your views, and explicitly tell them to display that data. The simplest way is with SharedPreferences:

// or use a separate prefs file if you want - see the SharedPreferences docs
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
with(prefs.edit()) {
    // since resource IDs are just ints, store as one
    putInt("image res ID", quote)
    apply()
}

And then in your Activity or whatever

val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val quote = prefs.getInt("image res ID", R.drawable.some_default)
imageView.setImageResource(quote)

That "image res ID" key should really be a top-level const val somewhere that both classes can see, so you're not retyping a string in both places.


Also you don't actually need to mess around with receivers or anything - does this really need to run when your app's not open? Or can you do it when the app is opened? Store your current image ID like here, and also store a timestamp for when the next change should happen. When the Activity is opened, see if the timestamp has passed - if so, pick a new random image and store it, and update the timestamp for the next refresh. Otherwise just use the stored image

If you do this in onResume or something, you can have it update when the user moves away from your app (or part of it) if necessary. You could even use coroutines or something to schedule an image change the moment it hits midnight, while the app is in use. You could do that with a broadcast receiver, but it's a lot more work getting access to the running Activity - easier to just do it all in the Activity itself (or in things it's hosting, like Fragments and ViewModels)

  • Related