Search code examples
androidservicepicasso

Load the image with picasso in a service class


I am struck on a point in my android app. I am developing an app in which i am using a service class and i m recieving notification through my service class. Now there is a scenario i want to change the wallpaper of my application of my phone and i am successfully doing this in my code but same when i place in service class the application crashes and give error picasso should run in main thread. Actually there is a scenario in my application if i recieve the notification even my app is in background then wallpaper should be changed. Here is my code:

public class FirebaseMessagingServices extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
      RemoteMessage.Notification notification = remoteMessage.getNotification();

        if (data.isEmpty()) {
            System.out.println("FCM type is Notification");
            parseNotificationMessage(notification);

        }
        else {
            System.out.println("FCM type is Data");
            parseDataMessage(remoteMessage);
        }

    }

Here i am changing the recieving the message and on recieve i chaeck the message and generate notification

    private void parseDataMessage(RemoteMessage remoteMessage) {
        String notification_title =remoteMessage.getData().get("title");

        String notification_message = remoteMessage.getData().get("message");

        if(notification_title.contains("wallpaper"))
        {
            changeWallpaper(notification_message);
        }
  else
        {
            generateNotification(remoteMessage.getData().get("title"), 
            remoteMessage.getData().get("message"));
        }

}

 public void changeWallpaper(String url)
    {
              final WallpaperManager myWallpaperManager
                = WallpaperManager.getInstance(activity);
        Picasso.with(activity).load(url).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                Log.i("ok", "The image was obtained correctly");
                try {
                    myWallpaperManager.setBitmap(bitmap);
                    Toast.makeText(activity, "Image has been set as Desktop Wallpaper", Toast.LENGTH_SHORT).show();
                 //   ((Activity) activity).findViewById(R.id.progressbar_wallpaper).setVisibility(View.GONE);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
                Log.e("ok", "The image was not obtained");
            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
                Log.e("ok", "Getting ready to get the image");
                //Here you should place a loading gif in the ImageView
                //while image is being obtained.
            }
        });

    }

This is successfully changing the wallpaper on main thread but i want to change the wallpaper in service class even when app is in background. Any kind of help would be appreciated


Solution

  • I solved this issue by using glide libraray in place of picasso and it works smoothly even when application is in background

    final WallpaperManager myWallpaperManager
                    = WallpaperManager.getInstance(this);
    
            Glide.with(getApplicationContext())
                    .asBitmap()
                    .load(url)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                            try {
                                myWallpaperManager.setBitmap(resource);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
    
                        }
    
                    });