Example usage for android.text ClipboardManager setText

List of usage examples for android.text ClipboardManager setText

Introduction

In this page you can find the example usage for android.text ClipboardManager setText.

Prototype

public abstract void setText(CharSequence text);

Source Link

Document

Sets the contents of the clipboard to the specified text.

Usage

From source file:org.smssecure.smssecure.ConversationFragment.java

private void handleCopyMessage(final Set<MessageRecord> messageRecords) {
    List<MessageRecord> messageList = new LinkedList<>(messageRecords);
    Collections.sort(messageList, new Comparator<MessageRecord>() {
        @Override//from  w  w  w . j  a  v  a  2s. c om
        public int compare(MessageRecord lhs, MessageRecord rhs) {
            if (lhs.getDateReceived() < rhs.getDateReceived())
                return -1;
            else if (lhs.getDateReceived() == rhs.getDateReceived())
                return 0;
            else
                return 1;
        }
    });

    StringBuilder bodyBuilder = new StringBuilder();
    ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
    boolean first = true;

    for (MessageRecord messageRecord : messageList) {
        String body = messageRecord.getDisplayBody().toString();

        if (body != null) {
            if (!first)
                bodyBuilder.append('\n');
            bodyBuilder.append(body);
            first = false;
        }
    }

    String result = bodyBuilder.toString();

    if (!TextUtils.isEmpty(result))
        clipboard.setText(result);
}

From source file:com.nnm.smsviet.MessageListActivity.java

/**
 * {@inheritDoc}// w ww .j a va2 s  .co  m
 */
public final boolean onItemLongClick(final AdapterView<?> parent, final View view, final int position,
        final long id) {
    final Context context = this;
    final Message m = Message.getMessage(this, (Cursor) parent.getItemAtPosition(position));
    final Uri target = m.getUri();
    final int read = m.getRead();
    final int type = m.getType();
    Builder builder = new Builder(context);
    builder.setTitle(R.string.message_options_);

    final Contact contact = this.conv.getContact();
    final String a = contact.getNumber();
    Log.d(TAG, "p: " + a);
    final String n = contact.getName();

    String[] items = this.longItemClickDialog;
    if (TextUtils.isEmpty(n)) {
        items[WHICH_VIEW_CONTACT] = this.getString(R.string.add_contact_);
    } else {
        items[WHICH_VIEW_CONTACT] = this.getString(R.string.view_contact_);
    }
    items[WHICH_CALL] = this.getString(R.string.call) + " " + contact.getDisplayName();
    if (read == 0) {
        items = items.clone();
        items[WHICH_MARK_UNREAD] = context.getString(R.string.mark_read_);
    }
    if (type == Message.SMS_DRAFT) {
        items = items.clone();
        items[WHICH_FORWARD] = context.getString(R.string.send_draft_);
    }
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(final DialogInterface dialog, final int which) {
            Intent i = null;
            switch (which) {
            case WHICH_VIEW_CONTACT:
                if (n == null) {
                    i = ContactsWrapper.getInstance().getInsertPickIntent(a);
                    Conversation.flushCache();
                } else {
                    final Uri u = MessageListActivity.this.conv.getContact().getUri();
                    i = new Intent(Intent.ACTION_VIEW, u);
                }
                try {
                    MessageListActivity.this.startActivity(i);
                } catch (ActivityNotFoundException e) {
                    Log.e(TAG, "activity not found: " + i.getAction(), e);
                    Toast.makeText(MessageListActivity.this, "activity not found", Toast.LENGTH_LONG).show();
                }
                break;
            case WHICH_CALL:
                MessageListActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("tel:" + a)));
                break;
            case WHICH_MARK_UNREAD:
                ConversationListActivity.markRead(context, target, 1 - read);
                MessageListActivity.this.markedUnread = true;
                break;
            case WHICH_REPLY:
                MessageListActivity.this
                        .startActivity(ConversationListActivity.getComposeIntent(MessageListActivity.this, a));
                break;
            case WHICH_FORWARD:
                int resId;
                if (type == Message.SMS_DRAFT) {
                    resId = R.string.send_draft_;
                    i = ConversationListActivity.getComposeIntent(MessageListActivity.this,
                            MessageListActivity.this.conv.getContact().getNumber());
                } else {
                    resId = R.string.forward_;
                    i = new Intent(Intent.ACTION_SEND);
                    i.setType("text/plain");
                    i.putExtra("forwarded_message", true);
                }
                CharSequence text = null;
                if (PreferencesActivity.decodeDecimalNCR(context)) {
                    text = Converter.convertDecNCR2Char(m.getBody());
                } else {
                    text = m.getBody();
                }
                i.putExtra(Intent.EXTRA_TEXT, text);
                i.putExtra("sms_body", text);
                context.startActivity(Intent.createChooser(i, context.getString(resId)));
                break;
            case WHICH_COPY_TEXT:
                final ClipboardManager cm = (ClipboardManager) context
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                if (PreferencesActivity.decodeDecimalNCR(context)) {
                    cm.setText(Converter.convertDecNCR2Char(m.getBody()));
                } else {
                    cm.setText(m.getBody());
                }
                break;
            case WHICH_VIEW_DETAILS:
                final int t = m.getType();
                Builder b = new Builder(context);
                b.setTitle(R.string.view_details_);
                b.setCancelable(true);
                StringBuilder sb = new StringBuilder();
                final String a = m.getAddress(context);
                final long d = m.getDate();
                final String ds = DateFormat.format(context.getString(R.string.DATEFORMAT_details), d)
                        .toString();
                String sentReceived;
                String fromTo;
                if (t == Calls.INCOMING_TYPE) {
                    sentReceived = context.getString(R.string.received_);
                    fromTo = context.getString(R.string.from_);
                } else if (t == Calls.OUTGOING_TYPE) {
                    sentReceived = context.getString(R.string.sent_);
                    fromTo = context.getString(R.string.to_);
                } else {
                    sentReceived = "ukwn:";
                    fromTo = "ukwn:";
                }
                sb.append(sentReceived + " ");
                sb.append(ds);
                sb.append("\n");
                sb.append(fromTo + " ");
                sb.append(a);
                sb.append("\n");
                sb.append(context.getString(R.string.type_));
                if (m.isMMS()) {
                    sb.append(" MMS");
                } else {
                    sb.append(" SMS");
                }
                b.setMessage(sb.toString());
                b.setPositiveButton(android.R.string.ok, null);
                b.show();
                break;
            case WHICH_DELETE:
                ConversationListActivity.deleteMessages(context, target, R.string.delete_message_,
                        R.string.delete_message_question, null);
                break;
            default:
                break;
            }
        }
    });
    builder.show();
    return true;
}

From source file:com.dwdesign.tweetings.fragment.StatusFragment.java

@SuppressLint({ "NewApi", "NewApi", "NewApi" })
@Override//from w ww  . j  av a2  s.c  o m
public boolean onMenuItemClick(final MenuItem item) {
    if (mStatus == null)
        return false;
    final String text_plain = mStatus.text_plain;
    final String screen_name = mStatus.screen_name;
    final String name = mStatus.name;
    switch (item.getItemId()) {
    case MENU_SHARE: {
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, "@" + mStatus.screen_name + ": " + text_plain);
        startActivity(Intent.createChooser(intent, getString(R.string.share)));
        break;
    }
    case MENU_RETWEET: {
        if (isMyRetweet(mStatus)) {
            mService.destroyStatus(mAccountId, mStatus.retweet_id);
        } else {
            final long id_to_retweet = mStatus.is_retweet && mStatus.retweet_id > 0 ? mStatus.retweet_id
                    : mStatus.status_id;
            mService.retweetStatus(mAccountId, id_to_retweet);
        }
        break;
    }
    case MENU_TRANSLATE: {
        translate(mStatus);
        break;
    }
    case MENU_QUOTE_REPLY: {
        final Intent intent = new Intent(INTENT_ACTION_COMPOSE);
        final Bundle bundle = new Bundle();
        bundle.putLong(INTENT_KEY_ACCOUNT_ID, mAccountId);
        bundle.putLong(INTENT_KEY_IN_REPLY_TO_ID, mStatusId);
        bundle.putString(INTENT_KEY_IN_REPLY_TO_SCREEN_NAME, screen_name);
        bundle.putString(INTENT_KEY_IN_REPLY_TO_NAME, name);
        bundle.putBoolean(INTENT_KEY_IS_QUOTE, true);
        bundle.putString(INTENT_KEY_TEXT, getQuoteStatus(getActivity(), screen_name, text_plain));
        intent.putExtras(bundle);
        startActivity(intent);
        break;
    }
    case MENU_QUOTE: {
        final Intent intent = new Intent(INTENT_ACTION_COMPOSE);
        final Bundle bundle = new Bundle();
        bundle.putLong(INTENT_KEY_ACCOUNT_ID, mAccountId);
        bundle.putBoolean(INTENT_KEY_IS_QUOTE, true);
        bundle.putString(INTENT_KEY_TEXT, getQuoteStatus(getActivity(), screen_name, text_plain));
        intent.putExtras(bundle);
        startActivity(intent);
        break;
    }
    case MENU_ADD_TO_BUFFER: {
        final Intent intent = new Intent(INTENT_ACTION_COMPOSE);
        final Bundle bundle = new Bundle();
        bundle.putLong(INTENT_KEY_ACCOUNT_ID, mAccountId);
        bundle.putBoolean(INTENT_KEY_IS_BUFFER, true);
        bundle.putString(INTENT_KEY_TEXT, getQuoteStatus(getActivity(), screen_name, text_plain));
        intent.putExtras(bundle);
        startActivity(intent);
        break;
    }
    case MENU_REPLY: {
        final Intent intent = new Intent(INTENT_ACTION_COMPOSE);
        final Bundle bundle = new Bundle();
        final List<String> mentions = new Extractor().extractMentionedScreennames(text_plain);
        mentions.remove(screen_name);
        mentions.add(0, screen_name);
        bundle.putStringArray(INTENT_KEY_MENTIONS, mentions.toArray(new String[mentions.size()]));
        bundle.putLong(INTENT_KEY_ACCOUNT_ID, mAccountId);
        bundle.putLong(INTENT_KEY_IN_REPLY_TO_ID, mStatusId);
        bundle.putString(INTENT_KEY_IN_REPLY_TO_TWEET, text_plain);
        bundle.putString(INTENT_KEY_IN_REPLY_TO_SCREEN_NAME, screen_name);
        bundle.putString(INTENT_KEY_IN_REPLY_TO_NAME, name);
        intent.putExtras(bundle);
        startActivity(intent);
        break;
    }
    case MENU_FAV: {
        if (mStatus.is_favorite) {
            mService.destroyFavorite(mAccountId, mStatusId);
        } else {
            mService.createFavorite(mAccountId, mStatusId);
        }
        break;
    }
    case MENU_COPY_CLIPBOARD: {
        final String textToCopy = "@" + mStatus.screen_name + ": " + mStatus.text_plain;
        int sdk = android.os.Build.VERSION.SDK_INT;
        if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(
                    Context.CLIPBOARD_SERVICE);
            clipboard.setText(textToCopy);
        } else {
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(
                    Context.CLIPBOARD_SERVICE);
            android.content.ClipData clip = android.content.ClipData.newPlainText("Status", textToCopy);
            clipboard.setPrimaryClip(clip);
        }
        Toast.makeText(getActivity(), R.string.text_copied, Toast.LENGTH_SHORT).show();
        break;
    }
    case MENU_DELETE: {
        mService.destroyStatus(mAccountId, mStatusId);
        break;
    }
    case MENU_EXTENSIONS: {
        final Intent intent = new Intent(INTENT_ACTION_EXTENSION_OPEN_STATUS);
        final Bundle extras = new Bundle();
        extras.putParcelable(INTENT_KEY_STATUS, mStatus);
        intent.putExtras(extras);
        startActivity(Intent.createChooser(intent, getString(R.string.open_with_extensions)));
        break;
    }
    case MENU_MUTE_SOURCE: {
        final String source = HtmlEscapeHelper.unescape(mStatus.source);
        if (source == null)
            return false;
        final Uri uri = Filters.Sources.CONTENT_URI;
        final ContentValues values = new ContentValues();
        final SharedPreferences.Editor editor = getSharedPreferences(SHARED_PREFERENCES_NAME,
                Context.MODE_PRIVATE).edit();
        final ContentResolver resolver = getContentResolver();
        values.put(Filters.TEXT, source);
        resolver.delete(uri, Filters.TEXT + " = '" + source + "'", null);
        resolver.insert(uri, values);
        editor.putBoolean(PREFERENCE_KEY_ENABLE_FILTER, true).commit();
        Toast.makeText(getActivity(), getString(R.string.source_muted, source), Toast.LENGTH_SHORT).show();
        break;
    }
    case MENU_SET_COLOR: {
        final Intent intent = new Intent(INTENT_ACTION_SET_COLOR);
        startActivityForResult(intent, REQUEST_SET_COLOR);
        break;
    }
    case MENU_CLEAR_COLOR: {
        clearUserColor(getActivity(), mStatus.user_id);
        updateUserColor();
        break;
    }
    case MENU_RECENT_TWEETS: {
        openUserTimeline(getActivity(), mAccountId, mStatus.user_id, mStatus.screen_name);
        break;
    }
    case MENU_FIND_RETWEETS: {
        openUserRetweetedStatus(getActivity(), mStatus.account_id,
                mStatus.retweet_id > 0 ? mStatus.retweet_id : mStatus.status_id);
        break;
    }
    default:
        return false;
    }
    return super.onOptionsItemSelected(item);
}

From source file:com.ichi2.anki.Info.java

/**
 * Copy debug information about the device to the clipboard
 * @return debugInfo// ww w  .ja va  2  s. c om
 */
public String copyDebugInfo() {
    StringBuilder sb = new StringBuilder();
    // AnkiDroid Version
    sb.append("AnkiDroid Version = ").append(AnkiDroidApp.getPkgVersionName()).append("\n\n");
    // Android SDK
    sb.append("Android Version = " + Build.VERSION.RELEASE).append("\n\n");
    // ACRA install ID
    sb.append("ACRA UUID = ").append(Installation.id(this)).append("\n");
    String debugInfo = sb.toString();
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText(debugInfo);
    return debugInfo;
}

From source file:com.juce.JuceAppActivity.java

public final void setClipboardContent (String newText)
{
    ClipboardManager clipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE);
    clipboard.setText (newText);
}

From source file:it.ciopper90.gojack2.MessageListActivity.java

/**
 * {@inheritDoc}/* www.  jav  a  2  s. co m*/
 */
public final boolean onItemLongClick(final AdapterView<?> parent, final View view, final int position,
        final long id) {
    final Context context = this;
    final Message m = Message.getMessage(this, (Cursor) parent.getItemAtPosition(position));
    final Uri target = m.getUri();
    final int read = m.getRead();
    final int type = m.getType();
    Builder builder = new Builder(context);
    builder.setTitle(R.string.message_options_);

    final Contact contact = this.conv.getContact();
    final String a = contact.getNumber();
    Log.d(TAG, "p: " + a);
    final String n = contact.getName();

    String[] items = this.longItemClickDialog;
    if (TextUtils.isEmpty(n)) {
        items[WHICH_VIEW_CONTACT] = this.getString(R.string.add_contact_);
    } else {
        items[WHICH_VIEW_CONTACT] = this.getString(R.string.view_contact_);
    }
    items[WHICH_CALL] = this.getString(R.string.call) + " " + contact.getDisplayName();
    if (read == 0) {
        items = items.clone();
        items[WHICH_MARK_UNREAD] = context.getString(R.string.mark_read_);
    }
    if (type == Message.SMS_DRAFT) {
        items = items.clone();
        items[WHICH_FORWARD] = context.getString(R.string.send_draft_);
    }
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(final DialogInterface dialog, final int which) {
            Intent i = null;
            switch (which) {
            case WHICH_VIEW_CONTACT:
                if (n == null) {
                    i = ContactsWrapper.getInstance().getInsertPickIntent(a);
                    Conversation.flushCache();
                } else {
                    final Uri u = MessageListActivity.this.conv.getContact().getUri();
                    i = new Intent(Intent.ACTION_VIEW, u);
                }
                try {
                    MessageListActivity.this.startActivity(i);
                } catch (ActivityNotFoundException e) {
                    Log.e(TAG, "activity not found: " + i.getAction(), e);
                    Toast.makeText(MessageListActivity.this, "activity not found", Toast.LENGTH_LONG).show();
                }
                break;
            case WHICH_CALL:
                MessageListActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("tel:" + a)));
                break;
            case WHICH_MARK_UNREAD:
                ConversationListActivity.markRead(context, target, 1 - read);
                MessageListActivity.this.markedUnread = true;
                break;
            case WHICH_REPLY:
                // MessageListActivity.this.startActivity(ConversationListActivity
                // .getComposeIntent(MessageListActivity.this, a));
                break;
            case WHICH_FORWARD:
                int resId;
                if (type == Message.SMS_DRAFT) {
                    resId = R.string.send_draft_;
                    // i =
                    // ConversationListActivity.getComposeIntent(MessageListActivity.this,
                    // MessageListActivity.this.conv.getContact().getNumber());
                } else {
                    resId = R.string.forward_;
                    i = new Intent(Intent.ACTION_SEND);
                    i.setType("text/plain");
                    i.putExtra("forwarded_message", true);
                }
                CharSequence text = null;
                if (PreferencesActivity.decodeDecimalNCR(context)) {
                    text = Converter.convertDecNCR2Char(m.getBody());
                } else {
                    text = m.getBody();
                }
                i.putExtra(Intent.EXTRA_TEXT, text);
                i.putExtra("sms_body", text);
                context.startActivity(Intent.createChooser(i, context.getString(resId)));
                break;
            case WHICH_COPY_TEXT:
                final ClipboardManager cm = (ClipboardManager) context
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                if (PreferencesActivity.decodeDecimalNCR(context)) {
                    cm.setText(Converter.convertDecNCR2Char(m.getBody()));
                } else {
                    cm.setText(m.getBody());
                }
                break;
            case WHICH_VIEW_DETAILS:
                final int t = m.getType();
                Builder b = new Builder(context);
                b.setTitle(R.string.view_details_);
                b.setCancelable(true);
                StringBuilder sb = new StringBuilder();
                final String a = m.getAddress(context);
                final long d = m.getDate();
                final String ds = DateFormat.format(context.getString(R.string.DATEFORMAT_details), d)
                        .toString();
                String sentReceived;
                String fromTo;
                if (t == Calls.INCOMING_TYPE) {
                    sentReceived = context.getString(R.string.received_);
                    fromTo = context.getString(R.string.from_);
                } else if (t == Calls.OUTGOING_TYPE) {
                    sentReceived = context.getString(R.string.sent_);
                    fromTo = context.getString(R.string.to_);
                } else {
                    sentReceived = "ukwn:";
                    fromTo = "ukwn:";
                }
                sb.append(sentReceived + " ");
                sb.append(ds);
                sb.append("\n");
                sb.append(fromTo + " ");
                sb.append(a);
                sb.append("\n");
                sb.append(context.getString(R.string.type_));
                if (m.isMMS()) {
                    sb.append(" MMS");
                } else {
                    sb.append(" SMS");
                }
                b.setMessage(sb.toString());
                b.setPositiveButton(android.R.string.ok, null);
                b.show();
                break;
            case WHICH_DELETE:
                ConversationListActivity.deleteMessages(context, target, R.string.delete_message_,
                        R.string.delete_message_question, null);
                break;
            default:
                break;
            }
        }
    });
    builder.show();
    return true;
}

From source file:com.intel.xdk.device.Device.java

public void copyToClipboard(String text) {
    ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText(text);
}

From source file:com.xgf.inspection.qrcode.google.zxing.client.CaptureActivity.java

private void handleDecodeInternally(Result rawResult, ResultHandler resultHandler, Bitmap barcode) {
    statusView.setVisibility(View.GONE);
    viewfinderView.setVisibility(View.GONE);
    resultView.setVisibility(View.VISIBLE);

    ImageView barcodeImageView = (ImageView) findViewById(R.id.barcode_image_view);
    if (barcode == null) {
        barcodeImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.qr_scan));
    } else {//from w w w  .ja  va2s  .com
        barcodeImageView.setImageBitmap(barcode);
    }

    TextView formatTextView = (TextView) findViewById(R.id.format_text_view);
    formatTextView.setText(rawResult.getBarcodeFormat().toString());

    TextView typeTextView = (TextView) findViewById(R.id.type_text_view);
    typeTextView.setText(resultHandler.getType().toString());

    DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
    String formattedTime = formatter.format(new Date(rawResult.getTimestamp()));
    TextView timeTextView = (TextView) findViewById(R.id.time_text_view);
    timeTextView.setText(formattedTime);

    TextView metaTextView = (TextView) findViewById(R.id.meta_text_view);
    View metaTextViewLabel = findViewById(R.id.meta_text_view_label);
    metaTextView.setVisibility(View.GONE);
    metaTextViewLabel.setVisibility(View.GONE);
    Map<ResultMetadataType, Object> metadata = rawResult.getResultMetadata();
    if (metadata != null) {
        StringBuilder metadataText = new StringBuilder(20);
        for (Map.Entry<ResultMetadataType, Object> entry : metadata.entrySet()) {
            if (DISPLAYABLE_METADATA_TYPES.contains(entry.getKey())) {
                metadataText.append(entry.getValue()).append('\n');
            }
        }
        if (metadataText.length() > 0) {
            metadataText.setLength(metadataText.length() - 1);
            metaTextView.setText(metadataText);
            metaTextView.setVisibility(View.VISIBLE);
            metaTextViewLabel.setVisibility(View.VISIBLE);
        }
    }

    TextView contentsTextView = (TextView) findViewById(R.id.contents_text_view);
    CharSequence displayContents = resultHandler.getDisplayContents();
    contentsTextView.setText(displayContents);
    // Crudely scale betweeen 22 and 32 -- bigger font for shorter text
    int scaledSize = Math.max(22, 32 - displayContents.length() / 4);
    contentsTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, scaledSize);

    TextView supplementTextView = (TextView) findViewById(R.id.contents_supplement_text_view);
    supplementTextView.setText("");
    supplementTextView.setOnClickListener(null);
    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(PreferencesActivity.KEY_SUPPLEMENTAL,
            true)) {
        SupplementalInfoRetriever.maybeInvokeRetrieval(supplementTextView, resultHandler.getResult(),
                historyManager, this);
    }

    int buttonCount = resultHandler.getButtonCount();
    ViewGroup buttonView = (ViewGroup) findViewById(R.id.result_button_view);
    buttonView.requestFocus();
    for (int x = 0; x < ResultHandler.MAX_BUTTON_COUNT; x++) {
        TextView button = (TextView) buttonView.getChildAt(x);
        if (x < buttonCount) {
            button.setVisibility(View.VISIBLE);
            button.setText(resultHandler.getButtonText(x));
            button.setOnClickListener(new ResultButtonListener(resultHandler, x));
        } else {
            button.setVisibility(View.GONE);
        }
    }

    if (copyToClipboard && !resultHandler.areContentsSecure()) {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        if (displayContents != null) {
            clipboard.setText(displayContents);
        }
    }
}

From source file:org.mozilla.gecko.GeckoAppShell.java

static void setClipboardText(final String text) {
    getHandler().post(new Runnable() {
        @SuppressWarnings("deprecation")
        public void run() {
            Context context = GeckoApp.mAppContext;
            if (Build.VERSION.SDK_INT >= 11) {
                android.content.ClipboardManager cm = (android.content.ClipboardManager) context
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setPrimaryClip(ClipData.newPlainText("Text", text));
            } else {
                android.text.ClipboardManager cm = (android.text.ClipboardManager) context
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(text);
            }/*w  w  w  .ja  v  a 2s  .c om*/
        }
    });
}

From source file:com.codebutler.farebot.activities.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    try {//ww  w.  ja v  a2  s  .c  om
        if (item.getItemId() == R.id.supported_cards) {
            startActivity(new Intent(this, SupportedCardsActivity.class));
            return true;

        } else if (item.getItemId() == R.id.about) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://codebutler.github.com/farebot")));
            return true;

        } else if (item.getItemId() == R.id.import_clipboard) {
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            onCardsImported(ExportHelper.importCardsXml(this, clipboard.getText().toString()));
            return true;

        } else if (item.getItemId() == R.id.import_file) {
            Uri uri = Uri.fromFile(Environment.getExternalStorageDirectory());
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.putExtra(Intent.EXTRA_STREAM, uri);
            i.setType("application/xml");
            startActivityForResult(Intent.createChooser(i, "Select File"), SELECT_FILE);
            return true;

        } else if (item.getItemId() == R.id.import_sd) {
            String xml = FileUtils.readFileToString(new File(SD_EXPORT_PATH));
            onCardsImported(ExportHelper.importCardsXml(this, xml));
            return true;

        } else if (item.getItemId() == R.id.copy_xml) {
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            clipboard.setText(ExportHelper.exportCardsXml(this));
            Toast.makeText(this, "Copied to clipboard.", 5).show();
            return true;

        } else if (item.getItemId() == R.id.share_xml) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, ExportHelper.exportCardsXml(this));
            startActivity(intent);
            return true;

        } else if (item.getItemId() == R.id.save_xml) {
            String xml = ExportHelper.exportCardsXml(this);
            File file = new File(SD_EXPORT_PATH);
            FileUtils.writeStringToFile(file, xml, "UTF-8");
            Toast.makeText(this, "Wrote FareBot-Export.xml to USB Storage.", 5).show();
            return true;

        } else if (item.getItemId() == R.id.prefs) {
            startActivity(new Intent(this, FareBotPreferenceActivity.class));
        }
    } catch (Exception ex) {
        Utils.showError(this, ex);
    }
    return false;
}