Code example for ClipboardManager

Methods: addPrimaryClipChangedListener, getPrimaryClip, getPrimaryClipDescription, hasPrimaryClip

0
	 
	@Override 
	public void onResume() { 
		super.onResume(); 
		 
		clipboard.addPrimaryClipChangedListener(this);
		checkClipboard(); 
	} 
 
	@Override 
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		 
		// It's not valid to run fragments transactions after this 
		handler.removeCallbacks(hideRunnable);
	} 
	 
	@Override 
	public void onDestroy() { 
		super.onDestroy(); 
		 
		// Make sure it's removed 
		handler.removeCallbacks(hideRunnable);
	} 
 
	@Override 
	public void onPrimaryClipChanged() { 
		Log.d(TAG, "Clipboard changed");
		 
		checkClipboard(); 
	} 
	 
	private void checkClipboard() { 
		if (currentUrl != null) {
			clipboardLinkView.setText(currentUrl);
		} else if (clipboard.hasPrimaryClip() && clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN)) {
			// Check that the clip is plain text 
			ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
			CharSequence text = item.getText();
			 
			// Check if it's a web url and that we have not previously seen it 
			if (!text.equals(lastUrl) && Patterns.WEB_URL.matcher(text).find() && !text.equals(currentUrl)) {
				Log.d(TAG, "New web url found: " + text);