Marsad
Marsad

Reputation: 921

Pass image from activity to class android (live wallpaper)

I am working on how to apply live wallpaper(GIF image). When I click on apply button the default gif image set as wallpaper. I'm getting all the images from firebase. So I want to set that image as wallpaper. I don't know how to pass that gif image from LiveViewActivity to GIFWallpaperService to set that .gif image as live wallpaper instead of default image. (sorry for my bad English, hope you understand)

LiveWallpaperActivity.java //main activity(where I'm getting all the images from firebase)

         |
         |  //pass the .gif image url by intent to next activity
         |

  LiveViewActivity.java

             |
             |
             | //Here I receive the image by intent and load into imageview with glide
             |
             | //added a button to apply live wallpaper(.gif image)
             | //pass .gif image to GIFWallpaperService class service (I don't know how to do)
             |
    GIFWallpaperService

LiveViewActivity

Where i add a button to apply live wallpaper

here i want to pass that .gif image to GIFWallpaperService

public class LiveViewActivity extends AppCompatActivity {

    ImageView imageView;
    Button setLiveWallpaper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_view);

        imageView = findViewById(R.id.viewImage);
        Glide.with(this).load(getIntent().getStringExtra("images")).into(imageView);

        setLiveWallpaper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                applyLiveWallpaper();
            }
        });

    }

    private void applyLiveWallpaper() {
        Intent intent = new Intent(
                WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
        intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
                new ComponentName(this, GIFWallpaperService.class));
        startActivity(intent);
    }

}

GIFWallpaperService

here I want to receive .gif image that I send from LiveViewActivity to set as live wallpaper


public class GIFWallpaperService extends WallpaperService {

    @Override
    public WallpaperService.Engine onCreateEngine() {

        try {

            Movie movie = Movie.decodeStream(getResources().getAssets().open("sea_gif.gif")); //Here is the default gif image

            return new GIFWallpaperEngine(movie);

        } catch (IOException e) {
            Log.d("GIFWallpaperService", "Could not loaded live wallpaper");
            return null;
        }

    }

    private class GIFWallpaperEngine extends WallpaperService.Engine {

        private final int frameDuration = 20;
        private SurfaceHolder holder;
        private Movie movie;
        private boolean visible;
        private Handler handler;

        public GIFWallpaperEngine(Movie movie) {

            this.movie = movie;
            handler = new Handler();

        }

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);

            this.holder = surfaceHolder;

        }

        private Runnable drawGIF = new Runnable() {
            @Override
            public void run() {
                draw();
            }
        };

        private void draw() {

            if (visible) {
                Canvas canvas = holder.lockCanvas();
                canvas.save();
                canvas.scale(4f, 4f);

                movie.draw(canvas, -100, 0);

                canvas.restore();

                holder.unlockCanvasAndPost(canvas);
                movie.setTime((int) (System.currentTimeMillis() % movie.duration()));

                handler.removeCallbacks(drawGIF);
                handler.postDelayed(drawGIF, frameDuration);

            }

        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            //super.onVisibilityChanged(visible);

            this.visible = visible;

            if (visible) {
                handler.post(drawGIF);
            } else {
                handler.removeCallbacks(drawGIF);
            }


        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            handler.removeCallbacks(drawGIF);

        }
    }

}


I don't know how to send and receive .gif image from LiveViewActivity to GIFWallpaperService

Upvotes: 4

Views: 1013

Answers (2)

Parisa Baastani
Parisa Baastani

Reputation: 1911

Your question is a little misty to me, but If I get you right, actually you can easily get the picture in LiveWallpaperActivity through the adapter by the context and then from this activity you can pass your image to whatever activity you want by intent.

To send a data from an activity to a service, you need to override onStartCommand there you have direct access to intent:

Override
public int onStartCommand(Intent intent, int flags, int startId) {

then from LiveViewActivity you will create the intent object to start service and then you place your image name inside the intent object :

 Intent serviceIntent = new Intent(YourService.class.getName())
 serviceIntent.putExtra("IMAGE_KEY", "image");
 context.startService(serviceIntent);

When the service is started its onStartCommand() method will be called then you can get the image:

public int onStartCommand (Intent intent, int flags, int startId) {
String image = intent.getStringExtra("IMAGE_KEY");
return START_STICKY;
}

Upvotes: 1

Jay
Jay

Reputation: 11

Try using DomainEventBus which helps in passing objects, images, and in fact any data types across all the applications. https://greenrobot.org/eventbus/

With this, you need to register the "event bus" on creation of service and it will be able to receive the image from any component of the application.

Upvotes: 0

Related Questions