Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;

public class Main {
    public static void copyToClipboard(@NonNull Context context, @NonNull String toCopy) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            copyToClipboard11AndLater(context, toCopy);
        } else {
            copyToClipboard10AndBefore(context, toCopy);
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private static void copyToClipboard11AndLater(@NonNull Context context, @NonNull String toCopy) {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", toCopy);
        clipboard.setPrimaryClip(clip);
    }

    private static void copyToClipboard10AndBefore(@NonNull Context context, @NonNull String toCopy) {
        @SuppressWarnings("deprecation")
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(toCopy);
    }
}