Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bubble cannot be run from the separated Service #40

Open
r0boto opened this issue Oct 12, 2017 · 3 comments
Open

Bubble cannot be run from the separated Service #40

r0boto opened this issue Oct 12, 2017 · 3 comments

Comments

@r0boto
Copy link

r0boto commented Oct 12, 2017

Hello,

I would like to ask how can I display bubble on the main thread in separated service (due to better code organization). I tried following, but when i started service using.

Intent myIntent = new Intent(mContext, BubbleService.class);
mContext.startService(myIntent);

No bubble is displayed, without any exception.

Should i use the IntentService for this or how can i solve this issue?

Many thanks for any advice.

BubbleService:

public class BubbleService extends Service {

    private Context mContext;
    private BubblesManager bubblesManager;
    private BubbleLayout currentBubbleLayout;
    private Boolean isBubbleDisplayed = false;
    private Handler mHandler;
    private Runnable mRunnable;

    public BubbleService() {
        mContext = this;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            Logger.d("onCreate");
            showBubble();
        } catch (Exception e) {
            Logger.e(e.getMessage());
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        //TODO: MOVE TO SEPARATED METHOD
        //bubblesManager.recycle();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    private void showBubble() {
        try {
            Logger.d("Show bubble");
            this.initBubble();
            BubbleLayout mBubbleView = (BubbleLayout) LayoutInflater
                    .from(BubbleService.this).inflate(R.layout.bubble_layout, null);
            //this.setTimerForInactiveBubble(mBubbleView); //TODO:UNCOMMENT AND RESET TIME WHEN USER TOUCHED THE BUBBLE
            mBubbleView.setOnBubbleClickListener(mBubbleClickListener);
            mBubbleView.setOnBubbleRemoveListener(mBubbleRemoveListener);
            if(!isBubbleDisplayed) {
                bubblesManager.addBubble(mBubbleView, 60, 20);
                isBubbleDisplayed = true;
            }

        } catch (Exception e) {
            Logger.e(e.getMessage());
        }
    }

    private void setTimerForInactiveBubble(final BubbleLayout bubbleLayout) {
        try {
            Logger.d("setTimerForInactiveBubble");
            mHandler = new Handler();
            mRunnable = new Runnable() {
                @Override
                public void run() {
                    mBubbleRemoveListener.onBubbleRemoved(bubbleLayout);
                }
            };
            mHandler.postDelayed(mRunnable,
                    Constants.Global.BUBBLE_DISMISS_DURATION);
            mHandler.removeCallbacks(mRunnable);
        } catch (Exception e) {
            Logger.e(e.getMessage());
        }
    }


    private BubbleLayout.OnBubbleRemoveListener mBubbleRemoveListener = new BubbleLayout.OnBubbleRemoveListener() {
        @Override
        public void onBubbleRemoved(BubbleLayout bubble) {
            Logger.d("onBubbleRemoved");
            bubble.removeAllViews();
            isBubbleDisplayed = false;
        }
    };

    private BubbleLayout.OnBubbleClickListener mBubbleClickListener = new BubbleLayout.OnBubbleClickListener() {
        @Override
        public void onBubbleClick(BubbleLayout bubble) {
            try {
                Logger.d("Clicked on the bubble");
                if (CommonHelper.isDeviceOnline(mContext)) {
                    bubble.findViewById(R.id.avatar).setVisibility(View.GONE);
                    bubble.findViewById(R.id.loading_progress_bar).setVisibility(View.VISIBLE);
                    currentBubbleLayout = bubble;
                }
                else {
                    ToastHelper.showToastMessage(mContext, //TODO: ADD THE OFFLINE TRANSACTION //TODO: MOVE TOAST TO HELPER?
                            mContext.getResources().getString(R.string.no_internet_connectivity),
                            Constants.Global.DEBUG_ENABLED);
                }
            } catch (Exception e) {
                Logger.d(e.getMessage());
            }

        }
    };

    private void initBubble() throws Exception {
        Logger.d("initBubble");
        bubblesManager = new BubblesManager.Builder(mContext)
                .setTrashLayout(R.layout.bubble_trash_layout)
                .build();
        bubblesManager.initialize();
    }
}

@Denny966
Copy link

Denny966 commented Jan 7, 2018

Same issue.

@goleary
Copy link

goleary commented Feb 16, 2018

Also trying to use a bubble from a separate service and nothing is displaying.

@pellyadolfo
Copy link

pellyadolfo commented Apr 22, 2018

I think the bubble is displayed, but is displayed so quick that is not seen. There is nothing wrong with the code displaying the bubbles.

The problem is in this line: https://github.com/txusballesteros/bubbles-for-android/blob/master/bubbles/src/main/java/com/txusballesteros/bubbles/BubblesService.java#L59

This line makes that, when the service unbinds, it removes the bubbles. And it unbinds after completing the task for any IntentServices (e.g. instances of FirebaseMessagingService). Just add some logging to your onDestroy method. So the bubble is shown and automatically removed in the line above.

By commenting this line out, it worked for me from a remote device's notification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants