Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'get(String!): Any?' is deprecated in Java, is there an alternative?

I've got a line of code like this:

val smsRetrieverStatus = extrasObj?.get(SmsRetriever.EXTRA_STATUS) as Status

But it's now deprecated as shown:

enter image description here

Is there any alternative way of doing this without getting deprecation warnings?

like image 665
Amy Avatar asked Apr 27 '26 03:04

Amy


2 Answers

If you read the method documentation here, it says:

This method was deprecated in API level 33. Use the type-safe specific APIs depending on the type of the item to be retrieved, eg. getString(java.lang.String)

So, they're telling you to stop using the generic get() method, and instead use a type-specific one -- getString(), getInt(), getLong(), etc.

Update: If you have a custom class or data type, then you'll need to implement your own logic to serialize that class (maybe use something like Gson). And once you've serialized it into a String, you can then use getString()

like image 178
user496854 Avatar answered Apr 28 '26 15:04

user496854


You can try another method

val status = extrasObj?.getParcelable(SmsRetriever.EXTRA_STATUS, Status::class.java)

Note that this method is not stable and sometimes throws NPE. So you'd better catch and handle the NPE.

like image 43
zhezha Avatar answered Apr 28 '26 15:04

zhezha