简体   繁体   English

当使用Floating Bubble作为后台服务拍摄实时屏幕截图时。 遇到两个错误

[英]When using Floating Bubble as a Background Service to take a screenshot of Live Screen. Getting two Errors

enter code here BackgroundService- enter code here BackgroundService-

    private void addNewBubble ()//ERROR , Expression expected and Missing ';' token{


        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        //here is all the science of params
        final WindowManager.LayoutParams myParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                PixelFormat.TRANSLUCENT
        );
        BubbleLayout bubbleView = (BubbleLayout) LayoutInflater.from(BackgroundService.this).inflate(R.layout.bubble_layout, null);
        bubbleView.setLayoutParams(myParams);

        bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() {
            @Override
            public void onBubbleRemoved(BubbleLayout bubble) {
            }
        });
        bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() {

            @Override
            public void onBubbleClick(BubbleLayout bubble) {


                Bitmap b = Screenshot.takescreenshotOfRootView(imageView);
                imageView.setImageBitmap(b);
                main.setBackgroundColor(Color.parseColor("#999999"));

                //Toast.makeText(getApplicationContext(), "Clicked !",
                //   Toast.LENGTH_SHORT).show();
            }
        });
        bubbleView.setShouldStickToWall(true);
        bubblesManager.addBubble(bubbleView, 60, 20);
    }
}

private void initializeBubblesManager() {
    bubblesManager = new BubblesManager.Builder(this)
            .setTrashLayout(R.layout.bubble_trash_layout)
            .setInitializationCallback(new OnInitializedCallback() {
                @Override
                public void onInitialized() {
                    addNewBubble();// ERROR
                }
            })
            .build();
    bubblesManager.initialize();
}

} }

This is the OnStart method which includes all the methods to create the floating bubble and to make it clickable to take a screenshot. 这是OnStart方法,其中包括创建浮动气泡并使其可单击以拍摄屏幕快照的所有方法。 Only addNewBubble is showing errors , whereas when the Floating Bubble code is run on the MainActivity without the creation of BackgroundService it runs fine without any errors. 只有addNewBubble显示错误,而在MainActivity上运行Floating Bubble代码而不创建BackgroundService时,它运行良好,没有任何错误。 Any suggestions as to what to do ? 有什么建议做什么?

Call addNewBubble() function in UiThread 在UiThread中调用addNewBubble()函数

runOnUiThread(new Runnable() {
      public void run() {
         addNewBubble()
      }
   });

like this. 像这样。

Copy paste this code .I have tested it 复制粘贴此代码。我已经测试过

 import android.content.Intent;
    import android.graphics.PixelFormat;
    import android.net.Uri;
    import android.os.Build;
    import android.os.Bundle;

    import android.provider.Settings;
    import android.support.v7.app.AppCompatActivity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.WindowManager;
    import android.widget.Toast;

    import com.txusballesteros.bubbles.BubbleLayout;
    import com.txusballesteros.bubbles.BubblesManager;
    import com.txusballesteros.bubbles.OnInitializedCallback;
    /**
     * Created by yohanson on 20/09/17.
     */

    public class MainActivity extends AppCompatActivity {

        private BubblesManager bubblesManager;
        private WindowManager windowManager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    checkDrawOverlayPermission();


                }
            });
        }


public void checkDrawOverlayPermission() {
    /** check if we already  have permission to draw over other apps */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            /** if not construct intent to request permission */
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            /** request permission via start activity for result */
            startActivityForResult(intent, 2);
        }
        else
        {
            initializeBubblesManager();
            addNewBubble();

        }
    }
}
        @Override
        protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
            /** check if received result code
             is equal our requested code for draw permission  */
            if (requestCode == 2) {
                initializeBubblesManager();
                addNewBubble();

            }
        }

        private void addNewBubble() {


            windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
            //here is all the science of params
            final WindowManager.LayoutParams myParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                    PixelFormat.TRANSLUCENT
            );
            BubbleLayout bubbleView = (BubbleLayout)LayoutInflater.from(MainActivity.this).inflate(R.layout.bubble_layout, null);
            bubbleView.setLayoutParams(myParams);

            bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() {
                @Override
                public void onBubbleRemoved(BubbleLayout bubble) { }
            });
            bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() {

                @Override
                public void onBubbleClick(BubbleLayout bubble) {
                    Toast.makeText(getApplicationContext(), "Clicked !",
                            Toast.LENGTH_SHORT).show();
                }
            });
            bubbleView.setShouldStickToWall(true);
            bubblesManager.addBubble(bubbleView, 60, 20);
        }

        private void initializeBubblesManager() {
            bubblesManager = new BubblesManager.Builder(this)
                    .setTrashLayout(R.layout.bubble_trash_layout)
                    .setInitializationCallback(new OnInitializedCallback() {
                        @Override
                        public void onInitialized() {
                            addNewBubble();
                        }
                    })
                    .build();
            bubblesManager.initialize();
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            bubblesManager.recycle();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用浮动气泡作为后台服务拍摄用户当前活动的屏幕截图。 有什么建议么? - take a screenshot of the current activity the user is on using a floating bubble running as a background service. Any Suggestions? 截取屏幕截图 - 后台服务 - Take a screenshot - Background Service 如何使用 MediaProjection API 从后台服务 class 截屏? - How to take a Screenshot from a background-service class using MediaProjection API? 如何将浮动气泡作为后台服务运行,将所有气泡代码放入OnStart方法中 - How to run a floating bubble as a background service , put all the bubble code inside OnStart method 在后台服务中截屏 - Taking screenshot in a Background Service 启动服务播放背景音乐时出错 - Starting a Service to play background music is getting errors 如何在Android中以编程方式截取整个屏幕 - How take screenshot of entire screen programmatically in Android 截取Android屏幕截图并保存到SD卡 - Take Screenshot of Android screen and save to SD card Android Gallery View在一个屏幕上显示两个视图。 - Android Gallery View showing two view on a single screen. 如何使用Selenium Web Drivver拍摄当前窗口的屏幕截图或模拟打印屏幕 - How to take screenshot of Current window or simulate print screen using selenium web drivver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM