以前我们曾经使用来获取SubscriptionManager的实例

SubscriptionManager subscriptionManager=SubscriptionManager.from(this);

但是在API 28中不推荐使用SubscriptionManager.from(context),获取SubscriptionManager实例的新方法是什么?

最佳答案

我们可以使用以下方式获取SubscriptionManager的实例

Java

 SubscriptionManager subscriptionManager= (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);

要么
SubscriptionManager subscriptionManager=getSystemService(SubscriptionManager.class);

对于API> = 23

Kotlin
val subscriptionManager = getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager

要么
var subscriptionManager = getSystemService(SubscriptionManager::class.java)

Official Documentation

关于android - SubscriptionManager.from()已弃用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52554713/

10-10 04:08