/**
* 判断一个动态壁纸是否已经在运行
* @param context:上下文
* @param tagetPackageName:要判断的动态壁纸的包名
* @return
*/
public static boolean isLiveWallpaperRunning(Context context,
String tagetPackageName) {
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(context);// 得到壁纸管理器
WallpaperInfo wallpaperInfo = wallpaperManager.getWallpaperInfo();// 如果系统使用的壁纸是动态壁纸话则返回该动态壁纸的信息,否则会返回null
if (wallpaperInfo != null) {// 如果是动态壁纸,则得到该动态壁纸的包名,并与想知道的动态壁纸包名做比较
String currentLiveWallpaperPackageName = wallpaperInfo
.getPackageName();
if (currentLiveWallpaperPackageName.equals(tagetPackageName)) {
Toast.makeText(context, "该动态壁纸已经在运行了", Toast.LENGTH_SHORT)
.show();
return true;
}
}
return false;
}
后来需求改了,因为要在一个应用中用到两个动态壁纸引擎,也就是两个wallpaperservice,而他们都在同一个应用中,也就是包名一样,想要切换不同的引擎要通过包来判断无法实现。于是想到另一种方法,通过服务名来判断,这样即使有多个不同的引擎提供商也能区分开来!
/**
* 判断一个动态壁纸是否已经在运行(根据service名来判断,因为一个动态壁纸主要就是一个service)
*
* @param context
* 上下文
* @param tagetServiceName
* 要判断的动态壁纸的服务名
* @return
*/
public static boolean isLiveWallpaperServiceRunning(Context context, String tagetServiceName) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);// 得到壁纸管理器
WallpaperInfo wallpaperInfo = wallpaperManager.getWallpaperInfo();// 如果系统使用的壁纸是动态壁纸话则返回该动态壁纸的信息,否则会返回null
if (wallpaperInfo != null) { // 如果是动态壁纸,则得到该动态壁纸的service名,并与想知道的动态壁纸<span style="font-size: 1em; line-height: 1.5;">service</span><span style="font-size: 1em; line-height: 1.5;">名做比较</span>
String currentLiveWallpaperServiceName = wallpaperInfo.getServiceName();
if (currentLiveWallpaperServiceName.equals(tagetServiceName)) {
return true;
}
}
return false;
}