Skip to content

Instantly share code, notes, and snippets.

@mkonicek
Created December 2, 2015 23:06
Show Gist options
  • Save mkonicek/37ab8d73e3d6102b8b03 to your computer and use it in GitHub Desktop.
Save mkonicek/37ab8d73e3d6102b8b03 to your computer and use it in GitHub Desktop.
import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.Context;
import android.os.Build;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
/**
* Pasteboard native module, allowing application to access native device clipboard.
*/
public class PasteboardModule extends ReactContextBaseJavaModule {
public PasteboardModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "Pasteboard";
}
@SuppressLint("DeprecatedMethod")
@ReactMethod
public void setPasteboardString(String text) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ClipData clipdata = ClipData.newPlainText(null, text);
ClipboardManager clipboard =
(ClipboardManager) getReactApplicationContext()
.getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setPrimaryClip(clipdata);
} else {
ClipboardManager textClipboard =
(ClipboardManager) getReactApplicationContext()
.getSystemService(Context.CLIPBOARD_SERVICE);
textClipboard.setText(text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment