I want to test the clipboard on the Android API, so I set out to create a simple application using the Android's ClipboardManager and ClipData classes. I followed the Copy and Paste guide on the Android website, although I had to fill in several spots as this guide doesn't have every line of code necessary (I eventually had to comment out the Intent and URI paste methods because it doesn't give concrete examples because of what I am assuming is variety in data, however). I run the application, yet for some reason, the text I am sending doesn't seem to work, or show up. Anyone know what might be causing this?
Here is my class:
public class MainActivity extends Activity
{
// Creates a URI based on a base URI and a crecord ID based on the contact's last name
// Declares the base URI string
private static final String CONTACTS = "content://com.example.contacts";
// Declares a path string for the URIs that you use to copy data
private static final String COPY_PATH = "/copy";
// Declares the URI to paste to the clipboard
Uri copyURI;
// Declares a MIME type constant to match against the MIME types offered by the provider
private static final String MIME_TYPE_CONTACT = "vnd.android.cursor.item/vnd.example.contact";
private int pasteType;
private Menu menu;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String lastName = "Nichols";
copyURI = Uri.parse(CONTACTS + COPY_PATH + "/" + lastName);
setContentView(R.layout.activity_main);
main();
}
void main()
{
String text = "Hi, bob!";
copyText(text);
String data = paste();
TextView tv = new TextView(this);
tv.setText(data);
setContentView(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.menu = menu;
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@TargetApi(11)
void copyIntent()
{
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
Intent appIntent = new Intent(this, com.example.clipboard.MainActivity.class);
ClipData clip = ClipData.newIntent("Intent", appIntent);
clipboard.setPrimaryClip(clip);
pasteType = 1;
}
@TargetApi(11)
void copyText(String text)
{
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple", text);
clipboard.setPrimaryClip(clip);
pasteType = 2;
}
@TargetApi(11)
void copyURI()
{
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newUri(getContentResolver(), "URI", copyURI);
clipboard.setPrimaryClip(clip);
pasteType = 3;
}
@TargetApi(11)
String paste()
{
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
/*if (pasteType == 1)
{
// Checks to see if the clip item contains an Intent, by testing to see if getIntent() returns null
Intent pasteIntent = clipboard.getPrimaryClip().getItemAt(0).getIntent();
if (pasteIntent != null)
{
}
}*/
if (pasteType == 2)
{
String pasteData = "";
// Gets the ID of the "paste" menu item
MenuItem mPasteItem = this.menu.findItem(R.id.paste);
// If the clipboard doesn't conatin data, disable the paste menu item
if (!(clipboard.hasPrimaryClip()))
mPasteItem.setEnabled(false);
else if (!(clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)))
{
// This disables the paste menu item, since the clipboard has data, but it is not plain text
mPasteItem.setEnabled(false);
}
else
mPasteItem.setEnabled(true);
// Examines the item on the clipboard. If getText() does not return null, the clip item contains the
// text. Assume the application can only handle one item at a time.
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
pasteData = (String) item.getText();
return pasteData;
}
/*if (pasteType == 3)
{
ContentResolver cr = getContentResolver();
ClipData clip = clipboard.getPrimaryClip();
if (clip != null)
{
ClipData.Item item = clip.getItemAt(0);
Uri pasteUri = item.getUri();
// If the clipboard contains a URI reference
if (pasteUri != null)
{
// Is this a content URI?
String uriMimeType = cr.getType(pasteUri);
// If the return value is not null, the Uri is a content URI
if (uriMimeType != null)
{
// Does the content provider offer a MIME type that the current application can use?
if (uriMimeType.equals(MIME_TYPE_CONTACT))
{
Cursor pasteCursor = cr.query(pasteUri, null, null, null, null);
if (pasteCursor != null)
{
if (pasteCursor.moveToFirst()) {
}
}
pasteCursor.close();
}
}
}
}
}*/
return null;
}
}
EDIT: Here is my menu.xml file, to help figure out why my menu isn't showing a copy/paste option.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>