Skip to content Skip to sidebar Skip to footer

How To Access The Clipboard On Android Device

My initial issue is being able to click a 'PASTE' bubble that pops up when the a click is being held on a text field. Currently I have not found a way to get that action to happen

Solution 1:

Android system only allow us to activate one Activity at a time, and the others are in onPause() state. Starting an activity should have a layout.xml, and must call startActivity(Intent).

From the logcat:

"java.lang.IllegalStateException: System services not available to Activities before onCreate()".

We can know that getSystemService() only available after super.onCreate(Bundle), which triggers the activity to be created.

A good practice to call getSystemService() in non-activity class is by passing Context parameter to GetClipBoard's constructor and make it as public:

publicclassGetClipBoardextendsAsyncTask<Void, Void, String> {

    privateContext context;

    publicGetClipBoard(Context context){
    this.context = context;
    }

    privateString pMyClip;
    @OverrideprotectedStringdoInBackground(Void...params) {
        try {
            // ClipboardManager p = params[0];String pasteData = "";
            ClipboardManager myClipBoard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            ...
        }catch (Exception e){
        ...
        }
        // Gets the clipboard as text.return pMyClip;
    }
...
}

So once you executing AsyncTask, call the class from Android components that has Context, e.g. Activity, Service, BroadcastReceiver, etc.

new GetClipBoard(this).execute(); // 'this' > context

Solution 2:

I believe that is my issue, currently I don't think I have a component that has Context. This is the class I am making the call from (part of it) and the first class that is being called by adb runtest.

publicclassSetupAppextendsUiAutomatorTestCase {


    publicvoidtestAppSetup()throws UiObjectNotFoundException, RemoteException
    { 

         //other code hereMyClipBoardmyBoard=newMyClipBoard();
         myBoard.getClipBoard(); 

Post a Comment for "How To Access The Clipboard On Android Device"