0

Help me

    private View mFloatingView;
    private View collapsedView;
    private View expandedView;
    private DigitalClock dateTimeDisplay;
    private final IBinder mBinder = new LocalBinder();
    public class LocalBinder extends Binder {
        ServiceMain getService() {
            return ServiceMain.this;
        }
    }

    public ServiceMain() {
       super();
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.v(TAG, "in onBind()");
        return mBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onCreate() {
        super.onCreate();
        layoutMain();
    }

    @Override
    public void onClick(View v) {

    }

    public void layoutMain() {
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        viewSuccess = layoutInflater.inflate(R.layout.toast_custom, null);
        prmSucces = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_SECURE,
                PixelFormat.TRANSLUCENT);
        prmSucces.gravity = Gravity.CENTER;
        viewLoading = layoutInflater.inflate(R.layout.popup_loading, null);
        prm = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_SECURE,
                PixelFormat.TRANSLUCENT);
        prm.gravity = Gravity.BOTTOM;
        prm.y = 200;
        mFloatingView = LayoutInflater.from(ServiceMain.this).inflate(R.layout.service_main, null);
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_SECURE,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 0;
        mWindowManager = (WindowManager) getBaseContext().getSystemService(WINDOW_SERVICE);
        mWindowManager.addView(mFloatingView, params);
        collapsedView = mFloatingView.findViewById(R.id.layoutCollapsed);
        expandedView = mFloatingView.findViewById(R.id.layout_main);
        final Button stop = mFloatingView.findViewById(R.id.btn_stop);
        final Button hide = mFloatingView.findViewById(R.id.btn_hide);
        final ImageView imvStart = mFloatingView.findViewById(R.id.imv_start);
        dateTimeDisplay = mFloatingView.findViewById(R.id.date);
        dateTimeDisplay.setTextColor(Color.WHITE);
        dateTimeDisplay.setTextSize(13);
        ;
        hide.setOnClickListener(this);
        stop.setOnClickListener(this);
        imvStart.setOnClickListener(this);
        mFloatingView.findViewById(R.id.relativeLayoutParent).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View p1) {

                stopSelf();
            }


        });
        mFloatingView.findViewById(R.id.relativeLayoutParent).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();
                        break;

                    case MotionEvent.ACTION_UP:
                        collapsedView.setVisibility(View.GONE);
                        expandedView.setVisibility(View.VISIBLE);
                        break;

                    case MotionEvent.ACTION_MOVE:
                        //this code is helping the widget to move around the screen with fingers
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        mWindowManager.updateViewLayout(mFloatingView, params);
                        break;
                }
                return true;
            }
        });
    }

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

2019-07-28 03:33:03.098 4694-4694/com.protectabnormalities.rcg E/AndroidRuntime: FATAL EXCEPTION: main Process: com.protectabnormalities.rcg, PID: 4694 java.lang.RuntimeException: Unable to create service com.protectabnormalities.rcg.service.ServiceMain: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? at android.app.ActivityThread.handleCreateService(ActivityThread.java:3582) at android.app.ActivityThread.access$1300(ActivityThread.java:200) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6718) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:798) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94) at com.protectabnormalities.rcg.service.ServiceMain.layoutMain(ServiceMain.java:110) at com.protectabnormalities.rcg.service.ServiceMain.onCreate(ServiceMain.java:68) at android.app.ActivityThread.handleCreateService(ActivityThread.java:3570) at android.app.ActivityThread.access$1300(ActivityThread.java:200)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6718)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Chris Gilardi
  • 1,116
  • 10
  • 23
  • Hi, can you elaborate a bit more about the context of your issue? – leojg Jul 27 '19 at 20:50
  • I experienced an error in Service, namely (Unable to add window - token null is not valid; is your activity running?) Even though before that there was no error smoothly, I was confused. – Rain City Gaming Jul 27 '19 at 23:48
  • I think this question has a issue similar to yours: https://stackoverflow.com/questions/18662239/android-view-windowmanagerbadtokenexception-unable-to-add-window-on-buider-s You seem to be trying to update the ui directly from the service, that won't work – leojg Jul 29 '19 at 07:16

0 Answers0