如要查看裝置的效能類別等級,請使用 Jetpack Core Performance 程式庫。這個程式庫會根據版本資訊中聲明的類別或來自 Google Play 服務的資料,回報裝置的媒體效能類別 (MPC) 等級。
請先在 Gradle 檔案中新增相關模組的依附元件:
Kotlin
// Implementation of Jetpack Core library.implementation("androidx.core:core-ktx:1.12.0")// Enable APIs to query for device-reported performance class.implementation("androidx.core:core-performance:1.0.0")// Enable APIs to query Google Play services for performance class.implementation("androidx.core:core-performance-play-services:1.0.0")
Groovy
// Implementation of Jetpack Core library.implementation'androidx.core:core-ktx:1.12.0'// Enable APIs to query for device-reported performance class.implementation'androidx.core:core-performance:1.0.0'// Enable APIs to query Google Play services for performance class.implementation'androidx.core:core-performance-play-services:1.0.0'
importandroidx.core.performance.play.services.PlayServicesDevicePerformanceclassMyApplication:Application(){lateinitvardevicePerformance:DevicePerformanceoverridefunonCreate(){// Use a class derived from the DevicePerformance interfacedevicePerformance=PlayServicesDevicePerformance(applicationContext)}}
Java
importandroidx.core.performance.play.services.PlayServicesDevicePerformance;classMyApplicationextendsApplication{DevicePerformancedevicePerformance;@OverridepublicvoidonCreate(){// Use a class derived from the DevicePerformance interfacedevicePerformance=newPlayServicesDevicePerformance(applicationContext);}}
classMyActivity:Activity(){privatelateinitvardevicePerformance:DevicePerformanceoverridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)// Note: Good app architecture is to use a dependency framework. See// https://developer.android.com/training/dependency-injection for more// information.devicePerformance=(applicationasMyApplication).devicePerformance}overridefunonResume(){super.onResume()when{devicePerformance.mediaPerformanceClass>=Build.VERSION_CODES.VANILLA_ICE_CREAM->{// MPC level 35 and later.// Provide the most premium experience for the highest performing devices.}devicePerformance.mediaPerformanceClass==Build.VERSION_CODES.UPSIDE_DOWN_CAKE->{// MPC level 34.// Provide a high quality experience.}else->{// MPC level 33, 31, 30, or undefined.// Remove extras to keep experience functional.}}}}
Java
classMyActivityextendsActivity{privateDevicePerformancedevicePerformance;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);// Note: Good app architecture is to use a dependency framework. See// https://developer.android.com/training/dependency-injection for more// information.devicePerformance=((MyApplication)getApplication()).devicePerformance;}@OverridepublicvoidonResume(){super.onResume();if(devicePerformance.getMediaPerformanceClass()>=Build.VERSION_CODES.VANILLA_ICE_CREAM){// MPC level 35 and later.// Provide the most premium experience for the highest performing devices.}elseif(devicePerformance.getMediaPerformanceClass()==Build.VERSION_CODES.UPSIDE_DOWN_CAKE){// MPC level 34.// Provide a high quality experience.}else{// MPC level 33, 31, 30, or undefined.// Remove extras to keep experience functional.}}}