简体   繁体   中英

how to change android wallpaper from outside of the activity class?

in the end I'm starting up a service that I will want to call to that class and change my phone's wallpaper.

in the mainActivity java file I can just write >

DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int height = metrics.heightPixels;
    int width = metrics.widthPixels;

Bitmap bmap2 = BitmapFactory.decodeResource(getResources(),   R.drawable.superman);
            Bitmap bitmap = Bitmap.createScaledBitmap(bmap2, width, height, true);

            WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
            // Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.superman);
            try {
                wallpaperManager.setBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }

and this works great... it uploads the wallpaper and starches it to fit screen

but I can't use it in another java class.,

how do I do this on another java class, and then later on call it from my myService Class ?

maybe don't use getWindowManager, from outside your activity ? since you can't use it outside activity class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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