Home > Back-end >  WindowManager Crash (Java/Android)
WindowManager Crash (Java/Android)

Time:12-24

I'm so tired doing this project
Im coding a project that show head-chat like Facebook
But when I add view to window manager, it don't show the head chat and crash
Please help me
MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.start_bubble).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startService(new Intent(MainActivity.this, ChatHeadService.class));
                finish();
            }
        });
    }
}

Here is my ChatHeadService.java code:

public class ChatHeadService extends Service {

    private WindowManager mWindowManager;
    private View mChatHeadView;

    public ChatHeadService() {
    }

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

    @Override
    public void onCreate() {
        super.onCreate();
        //Inflate the chat head layout we created
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mChatHeadView = LayoutInflater.from(this).inflate(R.layout.layout_chat_head, null);

        //Add the view to the window.
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        //Specify the chat head position
        params.gravity = Gravity.TOP | Gravity.LEFT;        //Initially view will be added to top-left corner
        params.x = 0;
        params.y = 100;

        //Add the view to the window
        mWindowManager.addView(mChatHeadView, params);

        //Set the close button.
        ImageView closeButton = (ImageView) mChatHeadView.findViewById(R.id.close_btn);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //close the service and remove the chat head from the window
                stopSelf();
            }
        });

        //Drag and move chat head using user's touch action.
        final ImageView chatHeadImage = (ImageView) mChatHeadView.findViewById(R.id.chat_head_profile_iv);
        chatHeadImage.setOnTouchListener(new View.OnTouchListener() {
            private int lastAction;
            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:

                        //remember the initial position.
                        initialX = params.x;
                        initialY = params.y;

                        //get the touch location
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();

                        lastAction = event.getAction();
                        return true;
                    case MotionEvent.ACTION_UP:
                        //As we implemented on touch listener with ACTION_MOVE,
                        //we have to check if the previous action was ACTION_DOWN
                        //to identify if the user clicked the view or not.
                        if (lastAction == MotionEvent.ACTION_DOWN) {
                            //Open the chat conversation click.
                            Intent intent = new Intent(ChatHeadService.this, ChatActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);

                            //close the service and remove the chat heads
                            stopSelf();
                        }
                        lastAction = event.getAction();
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        //Calculate the X and Y coordinates of the view.
                        params.x = initialX   (int) (event.getRawX() - initialTouchX);
                        params.y = initialY   (int) (event.getRawY() - initialTouchY);

                        //Update the layout with new X & Y coordinate
                        mWindowManager.updateViewLayout(mChatHeadView, params);
                        lastAction = event.getAction();
                        return true;
                }
                return false;
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mChatHeadView != null) mWindowManager.removeView(mChatHeadView);
    }
}

And here is my log:

2021-12-23 13:34:05.327 12661-12661/com.example.projectkhkt2021 I/DecorView: updateCaptionType >> DecorView@788d084[], isFloating=false, isApplication=true, hasWindowDecorCaption=false, hasWindowControllerCallback=true
2021-12-23 13:34:05.327 12661-12661/com.example.projectkhkt2021 D/DecorView: setCaptionType = 0, this = DecorView@788d084[]
2021-12-23 13:34:05.387 12661-12661/com.example.projectkhkt2021 D/InputTransport: Input channel constructed: 'f8e5316', fd=78
2021-12-23 13:34:05.389 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: setView = com.android.internal.policy.DecorView@788d084 TM=true
2021-12-23 13:34:05.418 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: assignNativeObject: nativeObject = 0 Surface(name=null)/@0x3316999 / android.view.SurfaceControl.readFromParcel:1117 android.view.IWindowSession$Stub$Proxy.relayout:1820 android.view.ViewRootImpl.relayoutWindow:9005 android.view.ViewRootImpl.performTraversals:3360 android.view.ViewRootImpl.doTraversal:2618 android.view.ViewRootImpl$TraversalRunnable.run:9971 android.view.Choreographer$CallbackRecord.run:1010 android.view.Choreographer.doCallbacks:809 android.view.Choreographer.doFrame:744 android.view.Choreographer$FrameDisplayEventReceiver.run:995 
2021-12-23 13:34:05.420 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: Relayout returned: old=(0,0,1080,2400) new=(0,0,1080,2400) req=(1080,2400)0 dur=11 res=0x7 s={true -5476376630825729904} ch=true fn=-1
2021-12-23 13:34:05.452 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1 1
2021-12-23 13:34:05.452 12661-12661/com.example.projectkhkt2021 D/InputMethodManager: prepareNavigationBarInfo() DecorView@788d084[MainActivity]
2021-12-23 13:34:05.452 12661-12661/com.example.projectkhkt2021 D/InputMethodManager: getNavigationBarColor() -855310
2021-12-23 13:34:05.454 12661-12661/com.example.projectkhkt2021 D/InputMethodManager: prepareNavigationBarInfo() DecorView@788d084[MainActivity]
2021-12-23 13:34:05.454 12661-12661/com.example.projectkhkt2021 D/InputMethodManager: getNavigationBarColor() -855310
2021-12-23 13:34:05.455 12661-12661/com.example.projectkhkt2021 V/InputMethodManager: Starting input: tba=com.example.projectkhkt2021 ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
2021-12-23 13:34:05.455 12661-12661/com.example.projectkhkt2021 D/InputMethodManager: startInputInner - Id : 0
2021-12-23 13:34:05.455 12661-12661/com.example.projectkhkt2021 I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
2021-12-23 13:34:05.458 12661-12661/com.example.projectkhkt2021 D/InputTransport: Input channel constructed: 'ClientS', fd=86
2021-12-23 13:34:05.474 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120561328]
2021-12-23 13:34:05.474 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120561328]
2021-12-23 13:34:05.474 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120557632]
2021-12-23 13:34:05.474 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120557632]
2021-12-23 13:34:10.047 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: ViewPostIme pointer 0
2021-12-23 13:34:10.080 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: ViewPostIme pointer 1
2021-12-23 13:34:10.133 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120539328]
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120539328]
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120557104]
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120557104]
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120560976]
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120560976]
2021-12-23 13:34:10.222 12661-12661/com.example.projectkhkt2021 D/InputTransport: Input channel destroyed: 'ClientS', fd=86
2021-12-23 13:34:10.236 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120440240]
2021-12-23 13:34:10.236 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120440240]
2021-12-23 13:34:10.473 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120417360]
2021-12-23 13:34:10.473 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120417360]
2021-12-23 13:34:10.474 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: assignNativeObject: nativeObject = 0 Surface(name=null)/@0x4770136 / android.view.SurfaceControl.readFromParcel:1117 android.view.IWindowSession$Stub$Proxy.relayout:1810 android.view.ViewRootImpl.relayoutWindow:9005 android.view.ViewRootImpl.performTraversals:3360 android.view.ViewRootImpl.doTraversal:2618 android.view.ViewRootImpl$TraversalRunnable.run:9971 android.view.Choreographer$CallbackRecord.run:1010 android.view.Choreographer.doCallbacks:809 android.view.Choreographer.doFrame:744 android.view.Choreographer$FrameDisplayEventReceiver.run:995 
2021-12-23 13:34:10.475 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: assignNativeObject: nativeObject = 0 Surface(name=null)/@0x3316999 / android.view.SurfaceControl.readFromParcel:1117 android.view.IWindowSession$Stub$Proxy.relayout:1820 android.view.ViewRootImpl.relayoutWindow:9005 android.view.ViewRootImpl.performTraversals:3360 android.view.ViewRootImpl.doTraversal:2618 android.view.ViewRootImpl$TraversalRunnable.run:9971 android.view.Choreographer$CallbackRecord.run:1010 android.view.Choreographer.doCallbacks:809 android.view.Choreographer.doFrame:744 android.view.Choreographer$FrameDisplayEventReceiver.run:995 
2021-12-23 13:34:10.477 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: Relayout returned: old=(0,0,1080,2400) new=(0,0,1080,2400) req=(1080,2400)8 dur=8 res=0x5 s={false 0} ch=true fn=23
2021-12-23 13:34:10.480 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: stopped(true) old=false
2021-12-23 13:34:10.489 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: dispatchDetachedFromWindow
2021-12-23 13:34:10.493 12661-12661/com.example.projectkhkt2021 D/InputTransport: Input channel destroyed: 'f8e5316', fd=78

Do u have any suggestions for me?

[I don't know why its said my post has too many code, I guest its because the log so I need to write something in here ]

CodePudding user response:

First you need to verify target device then it will work . here is sample code according to your service ,it will work fine.

public class ChatHeadService extends Service {

private WindowManager mWindowManager;
private View mChatHeadView;
int LAYOUT_FLAG;

public ChatHeadService() {
}

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

@Override
public void onCreate() {
    super.onCreate();
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
    }
    //Inflate the chat head layout we created
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mChatHeadView = LayoutInflater.from(this).inflate(R.layout.layout_chat_head, null);

    //Add the view to the window.
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            LAYOUT_FLAG,
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    //Specify the chat head position
    params.gravity = Gravity.TOP | Gravity.LEFT;        //Initially view will be added to top-left corner
    params.x = 0;
    params.y = 100;

    //Add the view to the window
    mWindowManager.addView(mChatHeadView, params);

    //Set the close button.
    ImageView closeButton = (ImageView) mChatHeadView.findViewById(R.id.close_btn);
    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //close the service and remove the chat head from the window
            stopSelf();
        }
    });

    //Drag and move chat head using user's touch action.
    final ImageView chatHeadImage = (ImageView) mChatHeadView.findViewById(R.id.chat_head_profile_iv);
    chatHeadImage.setOnTouchListener(new View.OnTouchListener() {
        private int lastAction;
        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:

                    //remember the initial position.
                    initialX = params.x;
                    initialY = params.y;

                    //get the touch location
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();

                    lastAction = event.getAction();
                    return true;
                case MotionEvent.ACTION_UP:
                    //As we implemented on touch listener with ACTION_MOVE,
                    //we have to check if the previous action was ACTION_DOWN
                    //to identify if the user clicked the view or not.
                    if (lastAction == MotionEvent.ACTION_DOWN) {
                        //Open the chat conversation click.
                        Intent intent = new Intent(ChatHeadService.this, ChatActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);

                        //close the service and remove the chat heads
                        stopSelf();
                    }
                    lastAction = event.getAction();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    //Calculate the X and Y coordinates of the view.
                    params.x = initialX   (int) (event.getRawX() - initialTouchX);
                    params.y = initialY   (int) (event.getRawY() - initialTouchY);

                    //Update the layout with new X & Y coordinate
                    mWindowManager.updateViewLayout(mChatHeadView, params);
                    lastAction = event.getAction();
                    return true;
            }
            return false;
        }
    });
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mChatHeadView != null) mWindowManager.removeView(mChatHeadView);
}
}
  • Related