Search code examples
androidserviceimageviewbroadcastreceiver

Why I am not able to use imageview in an activity which extends BroadcastReceiver?


Below is my ChatHeadService class and I am creating a new imageview in the oncreate() method and i can see the imageview.I am using it in the way similar to chat heads and my OnTouchListener is working but onClickListener is not working i want to open a new activity on clicking the imageview but it is not working even the toast doesn't appear.Thanks for any help in advance.

package com.tarun.notifyme2;

    import android.app.Service;
    import android.content.Intent;
    import android.graphics.PixelFormat;
    import android.os.IBinder;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.ImageView;
    import android.widget.Toast;

    public class ChatHeadService extends Service {

        private WindowManager windowManager;
        private ImageView chatHead;
        WindowManager.LayoutParams params;
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            int res = super.onStartCommand(intent, flags, startId);
            return res;
        }
        @Override
        public void onCreate() {
            super.onCreate();

            windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

            chatHead = new ImageView(this);
            chatHead.setImageResource(R.drawable.app_icon);
            chatHead.setClickable(true);
            params= new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_PHONE,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);

            params.gravity = Gravity.TOP | Gravity.LEFT;
            params.x = 0;
            params.y = 200;
            chatHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(),"You clicked  the imageview",Toast.LENGTH_LONG).show();
                    Log.i("tag","You clicked the imageview");
                    Intent i = new Intent(view.getContext(),SendNoti.class);
                    startActivity(i);
                    stopSelf();
                }
            });

            //this code is for dragging the chat head
            chatHead.setOnTouchListener(new View.OnTouchListener() {
                private int initialX;
                private int initialY;
                private float initialTouchX;
                private float initialTouchY;


                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;
                    case MotionEvent.ACTION_UP:
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        params.x = initialX
                                + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY
                                + (int) (event.getRawY() - initialTouchY);
                        windowManager.updateViewLayout(chatHead, params);
                        return true;
                    default:
                        return false;
                    }
                }
            });
            windowManager.addView(chatHead, params);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            if (chatHead != null)
                windowManager.removeView(chatHead);
            stopSelf();
        }

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    }

Solution

  • you are "swallowing" your touch events inside onTouch(View v, MotionEvent event) by returning true (means you are done all you needed and NOT notify onClick, onLongClick etc.). you may "emulate" onClick by setting some flag in ACTION_MOVE and check it in ACTION_UP if last "touch action" was moving or just down/up click (then return false or just write your code without any onClick set)