【问题标题】:Permission to record screen not showing outside the app in android允许录制屏幕未在 android 的应用程序外部显示
【发布时间】:2021-04-09 17:05:37
【问题描述】:

我正在开发一个简单的屏幕录制应用程序,它运行良好。 我还创建了一个浮动小部件,当用户打开应用程序以及应用程序在后台运行时会显示该小部件。

当我在应用程序中按下浮动小部件时,每次都会出现下面的对话框,如果我按下是,它将开始录制,我从谷歌播放商店下载的应用程序也会发生同样的情况。

但是当我离开我的应用程序时,如果我再次按下浮动小部件,它不会弹出上面的对话框,而我从 Play 商店下载的应用程序会弹出。而且我注意到我从 Play 商店下载的应用程序的浮动小部件隐藏了显示该权限对话框的更多信息。

这是我的显示浮动小部件的服务类

public class FloatingViewService extends Service {
    private WindowManager mWindowManager;
    private View mFloatingView;
    public static final String BROADCAST_ACTION = "com.mukesh.service";

    public FloatingViewService() {
    }

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

    @Override
    public void onCreate() {
        super.onCreate();
        //Inflate the floating view layout we created
        mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, 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.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        //Specify the view 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 = (WindowManager) getSystemService(WINDOW_SERVICE);
        mWindowManager.addView(mFloatingView, params);


        //….
        //….

        //The root element of the collapsed view layout
        final View collapsedView = mFloatingView.findViewById(R.id.collapse_view);
        //The root element of the expanded view layout
        final View expandedView = mFloatingView.findViewById(R.id.expanded_container);


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


        //Set the view while floating view is expanded.
        //Set the start recording button.
        ImageView playButton = (ImageView) mFloatingView.findViewById(R.id.startRecording);
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(FloatingViewService.this, "Recording started", Toast.LENGTH_LONG).show();
                //serviceCallbacks.onClick(1);

                Intent temp = new Intent(BROADCAST_ACTION);
                temp.putExtra("type" , 1);
                sendBroadcast(temp);
            }
        });


        //stop recording.
        ImageView nextButton = (ImageView) mFloatingView.findViewById(R.id.stopRecording);
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(FloatingViewService.this, "recording stoped.", Toast.LENGTH_LONG).show();
                Intent temp = new Intent(BROADCAST_ACTION);
                temp.putExtra("type" , 2);
                sendBroadcast(temp);
            }
        });


        //Set the close button
        ImageView closeButton = (ImageView) mFloatingView.findViewById(R.id.close_button);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collapsedView.setVisibility(View.VISIBLE);
                expandedView.setVisibility(View.GONE);
            }
        });


        //Drag and move floating view using user's touch action.
        mFloatingView.findViewById(R.id.root_container).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:


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


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

                    case MotionEvent.ACTION_UP:
                        int Xdiff = (int) (event.getRawX() - initialTouchX);
                        int Ydiff = (int) (event.getRawY() - initialTouchY);


                        //The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking.
                        //So that is click event.
                        if (Xdiff < 10 && Ydiff < 10) {
                            if (isViewCollapsed()) {
                                //When user clicks on the image view of the collapsed layout,
                                //visibility of the collapsed layout will be changed to "View.GONE"
                                //and expanded view will become visible.
                                collapsedView.setVisibility(View.GONE);
                                expandedView.setVisibility(View.VISIBLE);
                            }
                        }
                        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(mFloatingView, params);
                        return true;
                }
                return false;
            }
        });


    }


    private boolean isViewCollapsed() {
        return mFloatingView == null || mFloatingView.findViewById(R.id.collapse_view).getVisibility() == View.VISIBLE;
    }

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


}

任何建议,请

【问题讨论】:

    标签: android android-widget android-permissions android-windowmanager


    【解决方案1】:

    在 FloatingViewService 中添加foreground service 后,您的服务会更好地运行

    但是来自Android 10 there are some restrictions

    解决这些问题,您的服务将按照您的要求运作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多