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.
Many thanks for any advice.
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();
}
}