Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android clipboard code that works on all API levels

The clipboard code that works for API levels < 11 crashes on devices with API levels >= 11.

The clipboard code that work for API level >= 11 crashes on devices with API levels < 11.

I can not compile code for both versions because they have conflicting import requirements.

One needs: import android.text.ClipboardManager;

while the other needs: import android.content.ClipboardManager;

Surely there is a way write some code that will work on both sides of API level 11. I just can't figure it out.

***Edited (Since I can't answer my own question) *******

I found the problem. The exception message says, "Can't create handler inside a thread that has not called Looper.prepare()."

Apparently I have to jump through some more hoops since I am executing this code from an Async task.

like image 846
Xarph Avatar asked Jan 27 '12 00:01

Xarph


People also ask

How many API levels are there in Android?

Android exposes three Android API level project settings: Target Framework – Specifies which framework to use in building your application.

What is ClipData?

ClipData is a complex type containing one or more Item instances, each of which can hold one or more representations of an item of data. For display to the user, it also has a label. A ClipData contains a ClipDescription , which describes important meta-data about the clip.


2 Answers

I recently faced a similar problem. Here's how I handled it.

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB){
     android.content.ClipboardManager clipboard =  (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
        ClipData clip = ClipData.newPlainText("label", "Text to Copy");
        clipboard.setPrimaryClip(clip); 
} else{
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager)getSystemService(CLIPBOARD_SERVICE); 
    clipboard.setText("Text to Copy");
}
Toast.makeText(getApplicationContext(), "Text copied to clipboard", Toast.LENGTH_SHORT).show();

I'm not entirely sure if the first if block is necessary. But I'd rather not take a chance :)

like image 82
Ashok Goli Avatar answered Nov 15 '22 08:11

Ashok Goli


To avoid the exception

[FATAL EXCEPTION: GLThread 6132  java.lang.RuntimeException: 
Can't create handler inside thread that has not called Looper.prepare() ], 

-> just create the ClipboardManager once at startup, for example in your onCreate() method and use it anytime in a separate function.

tested working on 2.3.3 and 4.0.3:

import android.text.ClipboardManager;
import android.content.ClipData;
..
public class myActivity extends Activity
{
  private static ClipboardManager m_ClipboardManager;

  @Override
  protected void onCreate(..)
  {
    ...
   m_ClipboardManager = (ClipboardManager)     m_sInstance.getSystemService(Context.CLIPBOARD_SERVICE); 
  }

  public static void myCopyToClipBoard(String sTxt)
  {
    m_ClipboardManager.setText(sTxt);
  }
}
like image 27
Christophe LB Avatar answered Nov 15 '22 09:11

Christophe LB