2017-08-24 198 views
0

使用WallpaperManager.getInstance(context)(WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE)有什麼區別?WallpaperManager.getInstance()與getSystemService(Context.WALLPAPER_SERVICE)有什麼區別

如果我打開getInstance()方法的源代碼,它會返回(WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE),所以顯然沒有區別。但根據this answer,使用getInstance()解決了OP的問題。另外,docs建議使用getSystemService()

哪種方法更好,爲什麼?

回答

0

正如我所看到的,主要區別在於:由於getInstance()static方法,此實例僅創建一次,所有後續時間都使用此實例。所以當你撥打static getInstance()時,你可能會使用已經創建的實例。

+0

靜態方法並不意味着它返回對象的同一個實例。 – lagoman