Wallpaper is not setting up once set one wallpaper

69 views Asked by At

I have a video wallpaper app in which I am trying to set the live wallpaper for the first time it works fine. But the second time it does not change the wallpaper unless I restart the device.

I tried using "wallpapermanager.clear" function as well but the problem with that is if the user doesn't set the wallpaper the previous wallpaper gets removed automatically.

I don't want the previous one to get removed.

Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
vintent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
    new ComponentName(this, MyWallpaperService.class));
vstartActivity(intent);

try {
    WallpaperManager.getInstance(this).clear();
} catch (IOException e) {
    e.printStackTrace();
}
1

There are 1 answers

0
Gulnaz Naseer On

Save Your changes On SharedPreference of your WallPaper Class i.e MyWallpaperService use below code and modify according to your need hopefully its help... OnSharedPreferenceChangeListener use to save data both previous and updated you just need to set condition when you want update

class MyWallpaperService :WallpaperService() {

    /**
     * Service Starting Hierarchy
     *      1) Service:         onCreate()
     *      2) Service:         onCreateEngine()
     *          i) Engine:              onCreate()
     *          ii) Engine:             onSurfaceCreated()
     *          iii) Engine:            onSurfaceChanged()
     *          iv) Engine:             onVisibilityChanged()
     *          v) Engine:              onTouchEvent()                  // if any
     *          vi) Engine:             onSurfaceDestroyed()
     *          iii) Engine:            onDestroy()
     *      3) Service:         onDestroy()
     */

    override fun onCreateEngine(): Engine {
        return MyWallpaperEngine()
    }

    private inner class MyWallpaperEngine : Engine(), SharedPreferences.OnSharedPreferenceChangeListener {

        // A reference to our shared prefs;

        override fun onCreate(surfaceHolder: SurfaceHolder?) {
            super.onCreate(surfaceHolder)


            // Create an instance of CustomEngine
            val customEngine = MyWallpaperEngine()
           // Register the listener to start listening for shared preference changes
            customEngine.registerSharedPreferencesListener()

        }

        // Method to register the listener
        private fun registerSharedPreferencesListener() {
            val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
            sharedPreferences.registerOnSharedPreferenceChangeListener(this)
        }

        // Method to unregister the listener
        private fun unregisterSharedPreferencesListener() {
            val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
            sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
        }

        override fun onDestroy() {
            super.onDestroy()

            // Unregister the shared preferences listener to avoid memory leaks
            unregisterSharedPreferencesListener()
        }
        override fun onSharedPreferenceChanged(
            sharedPreferences: SharedPreferences?,
            key: String?
        ) {
           //your Changes data 
           //Rest of Code
        }
    }
}