0

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. 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. Any suggestions as to what to do ?

Yohanelly
  • 69
  • 1
  • 9
  • I am new to stackoverflow , please help me. I have checked here and cant seem to find a similar error. – Yohanelly Sep 20 '17 at 18:54
  • 2
    Possible duplicate of [Android permission denied for window type 2010 in Marshmallow or higher](https://stackoverflow.com/questions/37982167/android-permission-denied-for-window-type-2010-in-marshmallow-or-higher) – ventiseis Sep 20 '17 at 19:01
  • Yes I have read this , the error is similar. But my code is drastically different , I have used compile 'com.txusballesteros:bubbles:1.2.1' – Yohanelly Sep 20 '17 at 19:22
  • Hi Yohanelly, Stack Overflow isn't just a technical support forum. Show us specifics. Anyway, you might want to look [here](https://stackoverflow.com/questions/7569937)? – Nadav S. Sep 20 '17 at 21:13
  • Yes thank you , but i have added the android.permission.SYSTEM_ALERT_WINDOW in the manifest. Is there any other solution? – Yohanelly Sep 21 '17 at 05:26
  • Sorry guys I am new to android studio , I keep getting down voted. I dont mean to be so naive. Im sure this is a silly error to you guys. Id really appreciate some patience and help. Thank you – Yohanelly Sep 21 '17 at 07:06
  • hey @Yohanelly , please can you share a source code on how you were able to resolve ? – Ayodele Kayode Dec 03 '20 at 21:37

2 Answers2

1

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();
        }
    }
Saneesh
  • 1,796
  • 16
  • 23
  • Thank you so much , the application opens now. Although , the first time I opened it , it worked perfectly.I closed the app and ran it again. The app opens but the ADD BUBBLE button does not create a bubble again. Did this happen when you ran it? – Yohanelly Sep 21 '17 at 10:35
  • I forget to do the else part in checkDrawOverlayPermission(), I have edited the code .. just check it – Saneesh Sep 21 '17 at 10:41
  • So I used another emulator. Ive realized , it works the first time since when the app opens the first time it asks for permission. Then the bubble is created. However , opening the app for the second time , it does not ask for permission and so the app opens but the Add bubble button does not create a new bubble. Any way around this ? – Yohanelly Sep 21 '17 at 10:45
  • My apologies. It works. Thank you being so helpful and more importantly thanks for being so patient with me. – Yohanelly Sep 21 '17 at 10:49
  • Enjoy coding @Yohanelly – Saneesh Sep 21 '17 at 10:56
  • Hey @Saneesh so OnBubbleClick i want to take a screenshot of the view , Instead of showing a Toast message. Do you have any suggestions? – Yohanelly Sep 21 '17 at 11:23
  • Refer this one: https://stackoverflow.com/questions/2661536/how-to-programmatically-take-a-screenshot-in-android – Saneesh Sep 21 '17 at 11:35
  • Thsnk you , but this seems a little confusing. I want to make my bubble take a screenshot of the live screen. It doesnt matter if it is saved to the sd card or not. Can you please help me out if you have the time? – Yohanelly Sep 21 '17 at 13:42
  • I am now able to take a screenshot. But the screenshot only works within the application activity. I need to take a live screenshot. I need to create a background service which displays the floating bubble using the window manager. But being a novice, this task seems quite tough. Could you help me out? @Saneesh – Yohanelly Sep 21 '17 at 15:58
  • Hey I have come a long way since you last saw my code. I have created a background service and add all the bubble code to the OnStart() method. And only the permissions code remains in the MainActivity. Inside the checkDrawOverlayPermission() else statement I have put startService(new Intent(this, BackgroundService.class)); – Yohanelly Sep 22 '17 at 07:54
  • But I am getting only two errors. They seem simple enough to solve but I am Unable to do so. They are compile errors. Please could you have a look at the code? I will Edit the question and post the MainActivity and the BackgroundService code there. – Yohanelly Sep 22 '17 at 07:55
  • How to remove the remove bubble ? – Kalanidhi Aug 12 '19 at 07:04
0

Call addNewBubble() function in UiThread

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

like this.

Saneesh
  • 1,796
  • 16
  • 23