terlan abaszade
terlan abaszade

Reputation: 61

Android getParcelableExtra deprecated in api 33

I want to use intent method for get uri from another activity, but intent.getParcelableExtra is deprecated.if I use

  if (SDK_INT >= 33) {
    
        intent.getParcelableExtra("EXTRA_URI", Uri::class.java).let { ueray ->
                timeLineView.post({
                    if (ueray != null) {
                        setBitmap(ueray)
                        videoView.setVideoURI(ueray)
    
                    }
                })
            }
        }
        else {
            @Suppress("DEPRECATION")
       intent.getParcelableExtra<Uri>("EXTRA_URI").let { ueray ->
                timeLineView.post({
                    if (ueray != null) {
                        setBitmap(ueray)
                        videoView.setVideoURI(ueray)
    
                    }
                })
    
            }
        }

this code can google play reject my app? because when in remove (SDK_INT >= 33) statement it shows Call requires API level 33 (current min is 21): android.content.Intent#getParcelableExtra. Thanks in advance

Upvotes: 2

Views: 4615

Answers (4)

Marek Macko
Marek Macko

Reputation: 927

androidx.core:core-ktx provides IntentCompat class, with the following method:

IntentCompat.getParcelableExtra(intent, "EXTRA_URI", Uri::class.java)

To make it more convenient, you can create an extension:

inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? =
    IntentCompat.getParcelableExtra(this, key, T::class.java)

Upvotes: 2

blackapps
blackapps

Reputation: 9282

Instead of the uri put uri.toString() as an extra string.

Quite simple.

Upvotes: 0

sweak
sweak

Reputation: 1980

No, Google will not reject your app if you use deprecated method, especially when using it is a necessity as you have no other choice than to use it on SDK's < 33.

My app uses deprecated methods on lower SDK's when it is an only possibility and the app is fine and accessible on the Google Play Store:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val vibrationEffect = VibrationEffect.createWaveform(
        longArrayOf(1000, 1000),
        intArrayOf(255, 0),
        0
    )

    vibrator.vibrate(vibrationEffect, vibrationAudioAttributes)
} else {
    // deprecated but working on lower SDK's
    vibrator.vibrate(longArrayOf(0, 1000, 1000), 0, vibrationAudioAttributes)
}

Upvotes: 1

Nikola Despotoski
Nikola Despotoski

Reputation: 50588

These are extension functions for Intent and they are backward compatible:

@Suppress("DEPRECATION")
inline fun <reified P : Parcelable> Intent.getParcelable(key: String): P? {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        getParcelableExtra(key, P::class.java)
    } else {
        getParcelableExtra(key)
    }
}

@Suppress("DEPRECATION")
inline fun <reified P : Parcelable> Intent.getParcelableArrayList(key: String): ArrayList<P>? {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        getParcelableArrayListExtra(key, P::class.java)
    } else {
        getParcelableArrayListExtra(key)
    }
}
@Suppress("DEPRECATION")
inline fun <reified P : Parcelable> Bundle.getParcelableValue(key: String): P? {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        getParcelable(key, P::class.java)
    } else {
        getParcelable(key)
    }
}

@Suppress("DEPRECATION")
inline fun <reified P : Parcelable> Bundle.getParcelableArrayListValue(key: String): ArrayList<P>? {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        getParcelableArrayList(key, P::class.java)
    } else {
        getParcelableArrayList(key)
    }
}

Upvotes: 1

Related Questions