简体   繁体   English

Android获取墙纸名称

[英]Android get wallpaper name

I am developing an Android app for our company which sets the wallpaper to a specific company wallpaper every time the phone is booted. 我正在为我们公司开发一个Android应用程序,该应用程序在每次开机时都会将壁纸设置为特定的公司壁纸。 It would be preferable to check to see if the wallpaper has been changed rather than running the code to change the wallpaper. 最好检查墙纸是否已更改,而不是运行代码来更改墙纸。

Is there any way to get identifying information (eg filename etc.) from the current wallpaper? 有什么方法可以从当前墙纸获取标识信息(例如文件名等)?

WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
WallpaperInfo wallpaperInfo = wallpaperManager.getWallpaperInfo();

returns null for wallpaperInfo . wallpaperInfo返回null

Code for wallpaper change: 墙纸更换代码:

public static void setWallpaper(Context context) {

    // Has wallpaper changed?
    if (/*--wallpaperNotChanged--*/) {
        return;
    }

    try {

        // Setup
        Drawable drawable = context.getResources().getDrawable(R.drawable.test);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);

        // Get display sizes
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);

        // Create Bitmap
        Bitmap unscaledWallpaper = BitmapFactory.decodeResource(context.getResources(), R.drawable.test);
        Bitmap wallpaper = Bitmap.createScaledBitmap(unscaledWallpaper, displayMetrics.widthPixels, displayMetrics.heightPixels, true);

        // Set wallpaper
        wallpaperManager.setBitmap(wallpaper);

    } catch (Exception e){
        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

Please read this get-current-wallpaper . 请阅读此get-current-wallpaper And Get current wallpaper absolute path 获取当前墙纸的绝对路径

final Drawable wallpaperManager = wallpaperManager.getDrawable();

For better info you can visit: 有关更好的信息,您可以访问:
https://developer.android.com/reference/android/app/WallpaperInfo.html https://developer.android.com/reference/android/app/WallpaperInfo.html

WallpaperManager API Reference WallpaperManager API参考

public WallpaperInfo getWallpaperInfo ()

Here it says: 它说:

If the current wallpaper is a live wallpaper component , return the information about that wallpaper. 如果 当前墙纸是动态墙纸组件 ,请返回有关该墙纸的信息。 Otherwise, if it is a static image , simply return null . 否则, 如果它是静态图像 ,则只需返回null

You must be having a static image wallpaper in your background, not a live one. 您的背景中必须有静态图片墙纸,而不是动态墙纸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM