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
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)
}
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)
}
}
Instead of the uri put uri.toString() as an extra string.
Quite simple.
Related
Starting from API level 33 the getPackageInfo(String, int) method of PackageManager class is deprecated. Documentation suggests to use getPackageInfo(String, PackageInfoFlags) instead. But that function is only available from API level 33.
My current code:
val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)
Is this how it should be now?
val pInfo = context.getPackageInfo()
#Suppress("DEPRECATION")
fun Context.getPackageInfo(): PackageInfo {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0))
} else {
packageManager.getPackageInfo(packageName, 0)
}
}
If you are using Kotlin, you can add extension function to your project:
fun PackageManager.getPackageInfoCompat(packageName: String, flags: Int = 0): PackageInfo =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags.toLong()))
} else {
#Suppress("DEPRECATION") getPackageInfo(packageName, flags)
}
and after just call packageManager.getPackageInfoCompat(packageName) or add another flag, if you need.
Is this how it should be now?
Yes, though I've gotten out of the practice of using TIRAMISU in favor of the actual underlying Int.
Ideally, Google would add stuff to PackageManagerCompat for these changes, and perhaps they will now that Android 13 is starting to ship to users.
I am passing data via intent with Parcelable and receiving using getParcelableExtra . However getParcelableExtra seems to be deprecated, How do I fix the deprecation warning in this code? Alternatively, are there any other options for doing this? . I am using compileSdkVersion 33.
Code snippet:
var data = intent.getParcelableExtra("data")
Here are two extension methods that I use for Bundle & Intent:
inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? = when {
SDK_INT >= 33 -> getParcelableExtra(key, T::class.java)
else -> #Suppress("DEPRECATION") getParcelableExtra(key) as? T
}
inline fun <reified T : Parcelable> Bundle.parcelable(key: String): T? = when {
SDK_INT >= 33 -> getParcelable(key, T::class.java)
else -> #Suppress("DEPRECATION") getParcelable(key) as? T
}
I also requested this to be added to the support library
And if you need the ArrayList support there is:
inline fun <reified T : Parcelable> Bundle.parcelableArrayList(key: String): ArrayList<T>? = when {
SDK_INT >= 33 -> getParcelableArrayList(key, T::class.java)
else -> #Suppress("DEPRECATION") getParcelableArrayList(key)
}
inline fun <reified T : Parcelable> Intent.parcelableArrayList(key: String): ArrayList<T>? = when {
SDK_INT >= 33 -> getParcelableArrayListExtra(key, T::class.java)
else -> #Suppress("DEPRECATION") getParcelableArrayListExtra(key)
}
Now we need to use getParcelableExtra() with the type-safer class added to API 33
SAMPLE CODE For kotlin
val userData = if (VERSION.SDK_INT >= 33) {
intent.getParcelableExtra("DATA", User::class.java)
} else {
intent.getParcelableExtra<User>("DATA")
}
SAMPLE CODE For JAVA
if (android.os.Build.VERSION.SDK_INT >= 33) {
user = getIntent().getParcelableExtra("data", User.class);
} else {
user = getIntent().getParcelableExtra("data");
}
For example, in Java:
UsbDevice device;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.S_V2) { // TIRAMISU onwards
device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE, UsbDevice.class);
} else {
device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
}
This still requires #SuppressWarnings({"deprecation", "RedundantSuppression"}).
As described in the official documentation, getParcelableExtra was deprecated in API level 33.
So check if the API LEVEL is >= 33 or change the method,
...
if (Build.VERSION.SDK_INT >= 33) {
data = intent.getParcelableExtra (String name, Class<T> clazz)
}else{
data = intent.getParcelableExtra("data")
}
As the title says, i upgraded to API 31. I had a function to perform a vibration, but in the line
val vib = this.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
the VIBRATOR_SERVICE is now shown as deprecated. How can i replace it? Or at least, what's the modern solution for API 31 and above?
EDIT: as Joachim Sauer wrote, the alternative is VibrationManager. What i need now is the equivalent line of code using VibrationManager.
val vib = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager =
getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.defaultVibrator
} else {
#Suppress("DEPRECATION")
getSystemService(VIBRATOR_SERVICE) as Vibrator
}
The docs for this field say this:
This constant was deprecated in API level 31.
Use VibratorManager to retrieve the default system vibrator.
The most direct translation of code needing a Vibrator instance would be this:
val vibratorManager = this.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
val vibrator = vibratorManager.getDefaultVibrator();
Generally speaking whenever a class/method/field is deprecated like this then you should first check the documentation. Almost every single time it will tell you what to use instead (or in some cases that it has no replacement).
This code works for both old and new android devices. Reference to the docs Vibrate constantly for the specified period of time.. You should use a VibrationEffect instead to create the vibration pattern.
In Java:
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
final int DELAY = 0, VIBRATE = 1000, SLEEP = 1000, START = 0;
long[] vibratePattern = {DELAY, VIBRATE, SLEEP};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createWaveform(vibratePattern, START));
} else {
// backward compatibility for Android API < 26
// noinspection deprecation
vibrator.vibrate(vibratePattern, START);
}
In Kotlin:
val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
val DELAY = 0
val VIBRATE = 1000
val SLEEP = 1000
val START = 0
val vibratePattern = longArrayOf(DELAY.toLong(), VIBRATE.toLong(), SLEEP.toLong())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createWaveform(vibratePattern, START))
} else {
// backward compatibility for Android API < 26
// noinspection deprecation
vibrator.vibrate(vibratePattern, START)
}
Edit
This method works for API level 30 below properly, so to completely use this on API level 31 above you need to use VIBRATOR_MANAGER_SERVICE instead of VIBRATOR_SERVICE, to retrieve the default vibrator service.
The correct code is below (in Java) :
Vibrator vibrator;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
VibratorManager vibratorManager = (VibratorManager) getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
vibrator = vibratorManager.getDefaultVibrator();
} else {
// backward compatibility for Android API < 31,
// VibratorManager was only added on API level 31 release.
// noinspection deprecation
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
final int DELAY = 0, VIBRATE = 1000, SLEEP = 1000, START = 0;
long[] vibratePattern = {DELAY, VIBRATE, SLEEP};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createWaveform(vibratePattern, START));
} else {
// backward compatibility for Android API < 26
// noinspection deprecation
vibrator.vibrate(vibratePattern, START);
}
The correct code is below (in Kotlin) :
val vibrator: Vibrator
vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager: VibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.getDefaultVibrator()
} else {
// backward compatibility for Android API < 31,
// VibratorManager was only added on API level 31 release.
// noinspection deprecation
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
}
val DELAY = 0
val VIBRATE = 1000
val SLEEP = 1000
val START = 0
val vibratePattern = longArrayOf(DELAY.toLong(), VIBRATE.toLong(), SLEEP.toLong())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createWaveform(vibratePattern, START))
} else {
// backward compatibility for Android API < 26
// noinspection deprecation
vibrator.vibrate(vibratePattern, START)
}
Pulled together the various answers and cleaned them up to take into account changes in SDK 31 and 26, while providing backward compatibility.
#SuppressWarnings("deprecation")
private void vibrate() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
VibratorManager vibratorManager = (VibratorManager) getContext().getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
Vibrator vibrator = vibratorManager.getDefaultVibrator();
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
}
else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Vibrator vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
// API < 26
Vibrator vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
}
}
Handle SDK < 26, 26..32 and >= 33
private val vibrator: Vibrator by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
(getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager).defaultVibrator
} else {
#Suppress("DEPRECATION")
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
}
}
#SuppressLint("MissingPermission")
private fun startVibrator() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
vibrator.vibrate(
VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE),
VibrationAttributes.createForUsage(VibrationAttributes.USAGE_ALARM)
)
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
#Suppress("DEPRECATION")
vibrator.vibrate(
VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE),
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
)
} else {
#Suppress("DEPRECATION")
vibrator.vibrate(1000)
}
}
I created a wrapper class to handle the compatibility issue:
class VibratorHelper private constructor(private val context: Context) {
#Suppress("DEPRECATION")
fun vibrate(duration: Long) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.defaultVibrator.run {
cancel()
vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE))
}
} else {
val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.cancel()
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
vibrator.vibrate(duration)
}
}
}
companion object {
#JvmStatic
fun from(context: Context): VibratorHelper? {
val hasVibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.defaultVibrator.hasVibrator()
} else {
#Suppress("DEPRECATION")
val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.hasVibrator()
}
return if (hasVibrator) VibratorHelper(context.applicationContext) else null
}
}
}
Here's how to use it:
val vibrator = VibratorHelper.from(context)
vibrator?.vibrate(500)
this is simple answer for both old and new api
Give permission for vibration
<uses-permission android:name="android.permission.VIBRATE" />
After that use this code for kotlin
#Suppress("DEPRECATION")
private fun vibrate(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager = getSystemService(VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.defaultVibrator
} else {
val vibrator = getSystemService(VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(10)
}
}
after that just call the method
This is what I use in my app (Kotlin). It handles all the old versions and hides the deprecated warnings. It does one short vibrate.
fun AppCompatActivity.vibrate() {
val vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.defaultVibrator
} else {
#Suppress("DEPRECATION")
getSystemService(AppCompatActivity.VIBRATOR_SERVICE) as Vibrator
}
val duration = 200L
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
#Suppress("DEPRECATION")
vibrator.vibrate(duration)
}
}
I have written a simple Statment like this:
val installer = context.packageManager.getInstallerPackageName(context.packageName)
but it's now deprecated as shown in the picture:
Is there any alternative way available to get the package name of the app that has installed your app?
Here's how you can use the new one:
fun getInstallerPackageName(context: Context, packageName: String): String? {
kotlin.runCatching {
if (VERSION.SDK_INT >= VERSION_CODES.R)
return context.packageManager.getInstallSourceInfo(packageName).installingPackageName
#Suppress("DEPRECATION")
return context.packageManager.getInstallerPackageName(packageName)
}
return null
}
I created a helper function to check the remaining space of any given directory.
#RequiresApi(Build.VERSION_CODES.O)
fun Context.hasFreeSpace(directory: File, requiredStorageSpace: Long): Boolean{
return try {
val storageManager = getSystemService<StorageManager>()
val directoryUUID = storageManager!!.getUuidForPath(directory)
val availableBytes = storageManager.getAllocatableBytes(directoryUUID)
availableBytes > requiredStorageSpace
}catch (e: Exception){
e.printStackTrace()
false
}
}
Follow this link actually.
https://developer.android.com/training/data-storage/app-specific#query-free-space
The problem is I get storageManager!!.getUuidForPath and storageManager.getAllocatableBytes both require for API >= 26.
I did google around but not thing came back on how to get the directory's UUID on API < 26.
Does anyone have any idea how to achieve that?
Thanks
Well, I guess I need a different approach. As I googled, UUID required was added when Android O was released. So basically, no such thing gets directory UUID exits before O. This is my helper function now.
#SuppressLint("NewApi")
fun Context.hasFreeSpace(directory: File, requiredStorageSpace: Long): Boolean {
return try {
val api = Build.VERSION.SDK_INT
val availableBytes = when {
api >= Build.VERSION_CODES.O -> {
val storageManager = getSystemService<StorageManager>()
val directoryUUID = storageManager!!.getUuidForPath(directory)
storageManager.getAllocatableBytes(directoryUUID)
}
else -> {
val stat = StatFs(directory.path)
stat.availableBlocksLong * stat.blockSizeLong
}
}
availableBytes > requiredStorageSpace
} catch (e: Exception) {
e.printStackTrace()
false
}
}