Search code examples
javaandroidservicelive-wallpaperwallpaper

How to Stop Service If Wallpaper is Set from Another Application


I am using a service to change wallpaper after an interval and it works fine, Now the problem is "If user set a wallpaper from any other application that doesn't show because my application service is running in background" How can i Stop it if user set any other app wallpaper.

   public class Wallpaper extends Service { 
    SharedPreferences customSharedPreference;
    int img[] = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
            R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h,
            R.drawable.i, R.drawable.j, R.drawable.k, R.drawable.l,
            R.drawable.m, R.drawable.n, R.drawable.o };
    WallpaperManager wpm;

    int delay = 0;
    Timer timer;
    int index = -1;
    int int_delay = 500;

    public Wallpaper() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {

        delay = 10;
        customSharedPreference = getSharedPreferences(
                "com.janbark.live.wall.paper", Context.MODE_PRIVATE);
        int_delay = Integer.parseInt(customSharedPreference.getString(
                "userChoice", "500").toString());
        timer = new Timer();
    }

    @Override
    public void onStart(Intent intent, int startId) {

        timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                index++;
                if (int_delay != Integer.parseInt(customSharedPreference
                        .getString("userChoice", "500").toString())) {
                    timer.cancel();
                }
                Log.d("TAGDELAY", "" + int_delay);
                wpm = WallpaperManager.getInstance(getApplicationContext());

                if (index <= 14) {
                    if(!customSharedPreference.getBoolean("Live", true)){
                        timer.cancel();
                        stopService(new Intent(Wallpaper.this,Wallpaper.class));
                    }
                    try {
                        wpm.setResource(img[index]);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (index == 14)
                        index = -1;
                }

            }

        }, delay, int_delay);

    }

    @Override
    public void onDestroy() {

    }
}

Solution

  • If the wallpapers you're setting are live ones, and it appears that is the case, use this:

    boolean wallpaperWasSetByAnotherApp() {
         WallpaperManager wp_mngr = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
         WallpaperInfo info = wp_mngr.getWallpaperInfo();
         if (info == null) {
             // wallpaper is a static image
             return true; 
         } else {
             // wallpaper is live, check implementing service
             return !info.getComponent().equals(new ComponentName(this, getClass()));
         }
    }