android.content.ClipData Java Examples

The following examples show how to use android.content.ClipData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FeedbackActivity.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
@OnClick({R.id.feedback_by_qq_rl, R.id.feedback_by_qq_group_rl, R.id.feedback_more_rl})
public void onViewClicked(View view) {
    switch (view.getId()) {
        case R.id.feedback_by_qq_rl:
            String qq = qqTv.getText().toString();
            ClipboardManager qqCM = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData qqCD = ClipData.newPlainText("Label", qq);
            if (qqCM != null) {
                qqCM.setPrimaryClip(qqCD);
                ToastUtils.showShort("已复制QQ号:" + qq);
            }
            break;
        case R.id.feedback_by_qq_group_rl:
            String qqGroup = qqGroupTv.getText().toString();
            ClipboardManager qqGCM = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData qqGCD = ClipData.newPlainText("Label", qqGroup);
            if (qqGCM != null) {
                qqGCM.setPrimaryClip(qqGCD);
                ToastUtils.showShort("已复制QQ群号:" + qqGroup);
            }
            break;
        case R.id.feedback_more_rl:
            showDialog();
            break;
    }
}
 
Example #2
Source File: MainActivity.java    From user-interface-samples with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onLongClick(View v) {
    TextView thisTextView = (TextView) v;
    String dragContent = "Dragged Text: " + thisTextView.getText();

    //Set the drag content and type.
    ClipData.Item item = new ClipData.Item(dragContent);
    ClipData dragData = new ClipData(dragContent,
            new String[] {ClipDescription.MIMETYPE_TEXT_PLAIN}, item);

    //Set the visual look of the dragged object.
    //Can be extended and customized. Default is used here.
    View.DragShadowBuilder dragShadow = new View.DragShadowBuilder(v);

    // Starts the drag, note: global flag allows for cross-application drag.
    v.startDragAndDrop(dragData, dragShadow, null, DRAG_FLAG_GLOBAL);

    return false;
}
 
Example #3
Source File: CrashHandler.java    From a with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 收集错误信息.发送到服务器
 *
 * @return 处理了该异常返回true, 否则false
 */
private boolean handleException(Throwable ex) {
    if (ex == null) {
        return false;
    }
    //收集设备参数信息
    collectDeviceInfo(mContext);
    //添加自定义信息
    addCustomInfo();
    try {
        //复制错误报告到剪贴板
        ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = ClipData.newPlainText(null, ex.getMessage());
        if (clipboard != null) {
            clipboard.setPrimaryClip(clipData);
        }
        //使用Toast来显示异常信息
        new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(mContext, ex.getMessage(), Toast.LENGTH_LONG).show());
    } catch (Exception ignored) {
    }
    //保存日志文件
    saveCrashInfo2File(ex);
    return false;
}
 
Example #4
Source File: MainActivity.java    From LaunchTime with GNU General Public License v3.0 6 votes vote down vote up
private void startDragCategory(View view, String category) {
    ClipData data = ClipData.newPlainText(category, category);
    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
    //view.startDrag(data, shadowBuilder, view, 0);

    boolean dragstarted;
    if (Build.VERSION.SDK_INT>=24) {
        dragstarted = view.startDragAndDrop(data, shadowBuilder, view, 0);
    } else {
        dragstarted = view.startDrag(data, shadowBuilder, view, 0);
    }

    if (dragstarted) {
        mDragDropSource = mCategoriesLayout;
        if (!Categories.isSpeacialCategory(category)) {
            showRemoveDropzone();
        }
        showHiddenCategories();
    }
    cancelHide();
}
 
Example #5
Source File: Main2Activity.java    From styT with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) {
    if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null) return;
    Uri[] results = null;
    if (resultCode == AppCompatActivity.RESULT_OK) {
        if (intent != null) {
            String dataString = intent.getDataString();
            ClipData clipData = intent.getClipData();
            if (clipData != null) {
                results = new Uri[clipData.getItemCount()];
                for (int i = 0; i < clipData.getItemCount(); i++) {
                    ClipData.Item item = clipData.getItemAt(i);
                    results[i] = item.getUri();
                }
            }
            if (dataString != null)
                results = new Uri[]{Uri.parse(dataString)};
        }
    }
    uploadMessageAboveL.onReceiveValue(results);
    uploadMessageAboveL = null;
}
 
Example #6
Source File: ConfirmationActivity.java    From ETHWallet with GNU General Public License v3.0 6 votes vote down vote up
private void onTransaction(String hash) {
    hideDialog();
    dialog = new AlertDialog.Builder(this)
            .setTitle(R.string.transaction_succeeded)
            .setMessage(hash)
            .setPositiveButton(R.string.button_ok, (dialog1, id) -> {
                finish();
            })
            .setNeutralButton(R.string.copy, (dialog1, id) -> {
                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("transaction hash", hash);
                clipboard.setPrimaryClip(clip);
                finish();
            })
            .create();
    dialog.show();
}
 
Example #7
Source File: StandaloneActivity.java    From FireFiles with Apache License 2.0 6 votes vote down vote up
private void onFinished(Uri... uris) {
    Log.d(TAG, "onFinished() " + Arrays.toString(uris));
    final Intent intent = new Intent();
    if (uris.length == 1) {
        intent.setData(uris[0]);
    } else if (uris.length > 1) {
        final ClipData clipData = new ClipData(
                null, mState.acceptMimes, new ClipData.Item(uris[0]));
        for (int i = 1; i < uris.length; i++) {
            clipData.addItem(new ClipData.Item(uris[i]));
        }
        if(Utils.hasJellyBean()){
            intent.setClipData(clipData);
        }
        else{
            intent.setData(uris[0]);
        }
    }
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
            | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    setResult(Activity.RESULT_OK, intent);
    finish();
}
 
Example #8
Source File: ShooterSubActivity.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.about_download) {
        new CommonDialog.Builder(ShooterSubActivity.this)
                .setAutoDismiss()
                .setCancelListener(dialog -> {
                    String link = "https://secure.assrt.net/user/logon.xml";
                    ClipboardManager clipboardManagerMagnet = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData mClipDataMagnet = ClipData.newPlainText("Label", link);
                    if (clipboardManagerMagnet != null) {
                        clipboardManagerMagnet.setPrimaryClip(mClipDataMagnet);
                        ToastUtils.showShort("登录链接已复制");
                    }
                })
                .build()
                .show(getResources().getString(R.string.about_download_subtitle), "关于API", "确定", "复制登录链接");
    }
    return super.onOptionsItemSelected(item);
}
 
Example #9
Source File: CustomTabToolbar.java    From delion with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onLongClick(View v) {
    if (v == mCloseButton) {
        return showAccessibilityToast(v, getResources().getString(R.string.close_tab));
    } else if (v == mCustomActionButton) {
        return showAccessibilityToast(v, mCustomActionButton.getContentDescription());
    } else if (v == mTitleUrlContainer) {
        ClipboardManager clipboard = (ClipboardManager) getContext()
                .getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("url", mUrlBar.getText());
        clipboard.setPrimaryClip(clip);
        Toast.makeText(getContext(), R.string.url_copied, Toast.LENGTH_SHORT).show();
        return true;
    }
    return false;
}
 
Example #10
Source File: AccessUtil.java    From pc-android-controller-android with Apache License 2.0 6 votes vote down vote up
public static void inputText(Context context, AccessibilityService service, AccessibilityNodeInfo nodeInfo,
                                 final String hello) {
        //找到当前获取焦点的view
//        AccessibilityNodeInfo target = nodeInfo.findFocus(AccessibilityNodeInfo.FOCUS_INPUT);
        AccessUtil.findNodeInfosByName(nodeInfo, "android.widget.EditText");
        AccessibilityNodeInfo target = editText;
        if (target == null) {
            L.d("inputHello: null");

            return;
        } else {
            L.d("inputHello: not null " + target.getText());
        }
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("message", hello);
        clipboard.setPrimaryClip(clip);
        L.d("设置粘贴板");
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
//            target.performAction(AccessibilityNodeInfo.ACTION_PASTE);
//        }
        target.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
        L.d("获取焦点");
        target.performAction(AccessibilityNodeInfo.ACTION_PASTE);
        L.d("粘贴内容");
//        openNext2("发送", nodeInfo, service);//点击发送
    }
 
Example #11
Source File: Clipboard.java    From 365browser with Apache License 2.0 6 votes vote down vote up
public String clipDataToHtmlText(ClipData clipData) {
    ClipDescription description = clipData.getDescription();
    if (description.hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML)) {
        return clipData.getItemAt(0).getHtmlText();
    }

    if (description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
        CharSequence text = clipData.getItemAt(0).getText();
        if (!(text instanceof Spanned)) return null;
        Spanned spanned = (Spanned) text;
        if (hasStyleSpan(spanned)) {
            return ApiCompatibilityUtils.toHtml(
                    spanned, Html.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE);
        }
    }
    return null;
}
 
Example #12
Source File: SelectionPopupController.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public boolean canPasteAsPlainText() {
    // String resource "paste_as_plain_text" only exist in O.
    // Also this is an O feature, we need to make it consistant with TextView.
    if (!BuildInfo.isAtLeastO()) return false;
    if (!mCanEditRichlyForPastePopup) return false;
    ClipboardManager clipMgr =
            (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
    if (!clipMgr.hasPrimaryClip()) return false;

    ClipData clipData = clipMgr.getPrimaryClip();
    ClipDescription description = clipData.getDescription();
    CharSequence text = clipData.getItemAt(0).getText();
    boolean isPlainType = description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
    // On Android, Spanned could be copied to Clipboard as plain_text MIME type, but in some
    // cases, Spanned could have text format, we need to show "paste as plain text" when
    // that happens.
    if (isPlainType && (text instanceof Spanned)) {
        Spanned spanned = (Spanned) text;
        if (hasStyleSpan(spanned)) return true;
    }
    return description.hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML);
}
 
Example #13
Source File: SelectFileDialog.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@Override
protected void onPostExecute(Uri result) {
    mCameraOutputUri = result;
    if (mCameraOutputUri == null && captureCamera()) {
        onFileNotSelected();
        return;
    }

    Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    camera.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        camera.setClipData(ClipData.newUri(
                mWindowAndroid.getApplicationContext().getContentResolver(),
                UiUtils.IMAGE_FILE_PATH, mCameraOutputUri));
    }
    if (mDirectToCamera) {
        mWindow.showIntent(camera, mCallback, R.string.low_memory_error);
    } else {
        launchSelectFileWithCameraIntent(true, camera);
    }
}
 
Example #14
Source File: LogcatActivity.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onMenuItemClick(MenuItem item, LogLine logLine) {
    if (logLine != null) {
        switch (item.getItemId()) {
            case CONTEXT_MENU_COPY_ID:
                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

                clipboard.setPrimaryClip(ClipData.newPlainText(null, logLine.getOriginalLine()));
                Toast.makeText(this, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
                return true;
            case CONTEXT_MENU_FILTER_ID:

                if (logLine.getProcessId() == -1) {
                    // invalid line
                    return false;
                }

                showSearchByDialog(logLine);
                return true;
        }
    }
    return false;
}
 
Example #15
Source File: ClipBoardActivity.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
private void paste() {
        ClipData clipData = mClipboardManager.getPrimaryClip();
        ClipData.Item item = clipData.getItemAt(0);
        String content = item.getText().toString();
        pasteEdit.setText(content);

        Toast.makeText(this,"paste:"+content,Toast.LENGTH_SHORT).show();
}
 
Example #16
Source File: SettingsActivity.java    From AppOpsX with MIT License 5 votes vote down vote up
private void showShellStart(){
  AlertDialog.Builder builder =
      new AlertDialog.Builder(getActivity());
  builder.setMessage(getString(R.string.shell_cmd_help,getString(R.string.shell_cmd)));
  builder.setPositiveButton(android.R.string.copy, new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      ClipboardManager clipboardManager = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
      clipboardManager.setPrimaryClip(ClipData.newPlainText(null, getString(R.string.shell_cmd)));
      dialog.dismiss();
      Toast.makeText(getContext(),R.string.copied_hint,Toast.LENGTH_SHORT).show();
    }
  });
  builder.create().show();
}
 
Example #17
Source File: BugReportActivity.java    From Orin with GNU General Public License v3.0 5 votes vote down vote up
private void copyDeviceInfoToClipBoard() {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText(getString(R.string.device_info), deviceInfo.toMarkdown());
    clipboard.setPrimaryClip(clip);

    Toast.makeText(BugReportActivity.this, R.string.copied_device_info_to_clipboard, Toast.LENGTH_LONG).show();
}
 
Example #18
Source File: StatsActivity.java    From android with MIT License 5 votes vote down vote up
private void sendShare() {
    Uri imageUri = saveGraphImage();
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(
            R.string.share_subject));
    shareIntent.putExtra(Intent.EXTRA_TEXT,
            String.format(getResources().getString(R.string.share_content), mCallCount));
    shareIntent.setDataAndType(imageUri, "image/png");

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        // Needed to avoid security exception on KitKat.
        shareIntent.setClipData(ClipData.newRawUri(null, imageUri));
    }
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent,
            getResources().getString(R.string.share_chooser_title)));

    if (mTracker != null) {
        mTracker.send(new HitBuilders.EventBuilder()
                .setCategory("Share")
                .setAction("StatsShare")
                .setLabel(mCallCount + " calls")
                .setValue(1)
                .build());
    }

    startActivity(Intent.createChooser(shareIntent, getResources().getString(
            R.string.share_chooser_title)));
}
 
Example #19
Source File: ClipboardUtils.java    From QPM with Apache License 2.0 5 votes vote down vote up
/**
 * 获取剪贴板的文本
 *
 * @return 剪贴板的文本
 */
public static CharSequence getText() {
    ClipboardManager cm = (ClipboardManager) Utils.getApp().getSystemService(Context.CLIPBOARD_SERVICE);
    //noinspection ConstantConditions
    ClipData clip = cm.getPrimaryClip();
    if (clip != null && clip.getItemCount() > 0) {
        return clip.getItemAt(0).coerceToText(Utils.getApp());
    }
    return null;
}
 
Example #20
Source File: ClipboardUtils.java    From FCM-for-Mojo with GNU General Public License v3.0 5 votes vote down vote up
public static boolean put(Context context, CharSequence str) {
    try {
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label", str);
        clipboard.setPrimaryClip(clip);
        return true;
    } catch (Exception ignored) {
        return false;
    }
}
 
Example #21
Source File: ClipboardManager.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
public static boolean copyToClipboard(Context context, String text)
{
    try
    {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        android.content.ClipData clip = android.content.ClipData.newPlainText(CLIPBOARD_LABEL, text);
        clipboard.setPrimaryClip(clip);
        return true;
    }
    catch (Exception e)
    {
        return false;
    }
}
 
Example #22
Source File: CopyToClipboardActivity.java    From Cirrus_depricated with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // get the clipboard system service
    ClipboardManager clipboardManager = (ClipboardManager) this.getSystemService(CLIPBOARD_SERVICE);
    
    // get the text to copy into the clipboard 
    Intent intent = getIntent();
    CharSequence text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
    
    // and put the text the clipboard
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        // API level >= 11 -> modern Clipboard
        ClipData clip = ClipData.newPlainText("Synox was here", text);
        ((android.content.ClipboardManager)clipboardManager).setPrimaryClip(clip);
        
    } else {
        // API level >= 11 -> legacy Clipboard
        clipboardManager.setText(text);    
    }
    
    // alert the user that the text is in the clipboard and we're done
    Toast.makeText(this, R.string.clipboard_text_copied, Toast.LENGTH_SHORT).show();
    
    finish();
}
 
Example #23
Source File: Html5Activity.java    From ClassSchedule with Apache License 2.0 5 votes vote down vote up
private void copyUrl() {
    ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    cm.setPrimaryClip(ClipData.newPlainText(mWebView.getTitle(), mWebView.getUrl()));
    Snackbar.make(mLayout, "复制成功!", Snackbar.LENGTH_SHORT).setAction("确定",
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            }).show();
}
 
Example #24
Source File: DragEvent.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void init(int action, float x, float y, ClipDescription description, ClipData data,
        IDragAndDropPermissions dragAndDropPermissions, Object localState, boolean result) {
    mAction = action;
    mX = x;
    mY = y;
    mClipDescription = description;
    mClipData = data;
    this.mDragAndDropPermissions = dragAndDropPermissions;
    mLocalState = localState;
    mDragResult = result;
}
 
Example #25
Source File: TxtRecordsAdapter.java    From BonjourBrowser with Apache License 2.0 5 votes vote down vote up
public void onItemClick(View view, int position){
    Context context = view.getContext();

    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText(getKey(position), getValue(position));
    clipboard.setPrimaryClip(clip);

    Snackbar snackbar = Snackbar.make(view, context.getResources().getString(R.string.copy_toast_message, getKey(position)), Snackbar.LENGTH_LONG);
    snackbar.getView().setBackgroundResource(R.color.accent);
    snackbar.show();
}
 
Example #26
Source File: ClipboardService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private final void grantItemLocked(ClipData.Item item, int sourceUid, String targetPkg,
        int targetUserId) {
    if (item.getUri() != null) {
        grantUriLocked(item.getUri(), sourceUid, targetPkg, targetUserId);
    }
    Intent intent = item.getIntent();
    if (intent != null && intent.getData() != null) {
        grantUriLocked(intent.getData(), sourceUid, targetPkg, targetUserId);
    }
}
 
Example #27
Source File: ImageDragListener.java    From user-interface-samples with Apache License 2.0 5 votes vote down vote up
private boolean processDrop(View view, DragEvent event) {
    ClipData clipData = event.getClipData();
    if (clipData == null || clipData.getItemCount() == 0) {
        return false;
    }
    ClipData.Item item = clipData.getItemAt(0);
    if (item == null) {
        return false;
    }
    Uri uri = item.getUri();
    if (uri == null) {
        return false;
    }
    return setImageUri(view, event, uri);
}
 
Example #28
Source File: CopySeedDialog.java    From android-wallet-app with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Bundle bundle = getArguments();
    generatedSeed = bundle.getString("generatedSeed");

    final AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
            .setTitle(R.string.copy_seed)
            .setMessage(R.string.messages_copy_seed)
            .setCancelable(false)
            .setPositiveButton(R.string.buttons_ok, null)
            .setNegativeButton(R.string.buttons_cancel, null)
            .create();

    alertDialog.setOnShowListener(dialog -> {

        Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        button.setOnClickListener(view -> {
            ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText(getActivity().getString(R.string.seed), generatedSeed);
            clipboard.setPrimaryClip(clip);
            dialog.dismiss();
        });
    });

    alertDialog.show();
    return alertDialog;
}
 
Example #29
Source File: ClipboardService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void setPrimaryClip(ClipData clip, String callingPackage) {
    synchronized (this) {
        if (clip == null || clip.getItemCount() <= 0) {
            throw new IllegalArgumentException("No items");
        }
        final int callingUid = Binder.getCallingUid();
        if (!clipboardAccessAllowed(AppOpsManager.OP_WRITE_CLIPBOARD, callingPackage,
                    callingUid)) {
            return;
        }
        checkDataOwnerLocked(clip, callingUid);
        setPrimaryClipInternal(clip, callingUid);
    }
}
 
Example #30
Source File: AboutActivity.java    From PoetryWeather with Apache License 2.0 5 votes vote down vote up
private void copyToClipboard(String label, String string) {
    //复制
    ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clipData = ClipData.newPlainText(label, string);
    if (clipboardManager != null) {
        clipboardManager.setPrimaryClip(clipData);
        Toast.makeText(this, "成功复制" + label, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "获取剪贴板失败,无法复制", Toast.LENGTH_SHORT).show();
    }
}