1

Clipboard data returns null when MainActivity is not active in top view activity when I click the button in Android java

I read this restriction about android10 and higher, but my activity is not a background service. I need to get clipboard data when I click on the button located in the top view activity like the Google Translate application. In my case, the clipboard returns null when MainActivity is not active.

Google translate https://developer.android.com/about/versions/10/privacy/changes#clipboard-data

public class MainActivity extends BridgeActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Initializes the Bridge
        this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
          // Additional plugins you've installed go here
          // Ex: add(TotallyAwesomePlugin.class);
        }});
        
        startActivity(new Intent(MainActivity.this, FloatingWindow.class));
    }
}



public class FloatingWindow extends Activity {
// ... additional code ....
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        floatingButtonDefinedInClass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    self.getClipboardText(); 
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    public void getClipboardText() throws IOException {
        try {
            ClipboardManager myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
            ClipData clipData = myClipboard.getPrimaryClip();
            if(clipData != null) {
                text = (String) clipData.getItemAt(0).getText();
                System.out.println(text); // returns null when mainactivity is not active 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    
// ... additional code ....
}
Azat
  • 21
  • 3

1 Answers1

0

what does "mainactivity is not active" mean? you are getting access to ClipboardManager inside OnClickListener attached to Button, which is a View and need Context, so there must be alive Activity, which keeps this Button on the screen for clicking purpose...

btw. maybe you are you checking on Android 10 or above? it looks like according to docs

Limited access to clipboard data

Unless your app is the default input method editor (IME) or is the app that currently has focus, your app cannot access clipboard data on Android 10 or higher.

privacy/security reasons, thats how it will be working now

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Thank you for your reply @snachmsm, I meant when user switched to another application – Azat Nov 30 '20 at 14:04
  • is your code working on Android 9 and doesn't work on Android 10? – snachmsm Nov 30 '20 at 14:54
  • Yes, In android9 works perfectly, but in android10 not working – Azat Nov 30 '20 at 15:01
  • have you read linked docs? you can't do anything with this, it's safety feature. on iOS this is how it works since the beginning - only foreground window (`Activity` in Android ecosystem) can get access to clipboard – snachmsm Nov 30 '20 at 15:06
  • 1
    In that case how Google Translate works on Android 10? – Azat Nov 30 '20 at 15:11
  • first of all as I know Google Translate app you have to open it (is active) and then you may paste text there. another way - maybe `ForegroundService` can read clipboard and maybe Google Translate is using it (I doubt). last, but not least - Google apps oftenly have some additional necessary privileges as system apps or granted by system apps - Play store have huge possibilities and can share/grant some privileges/permissions during installation. e.g. check [THIS](https://stackoverflow.com/questions/36016369/system-alert-window-how-to-get-this-permission-automatically-on-android-6-0-an) topic – snachmsm Nov 30 '20 at 15:15