您的位置:首页 > 移动开发 > Android开发

Android的文本和输入---复制和粘贴(五)

2012-12-17 08:26 225 查看
复制结构性数据

为了复制和粘贴复杂的数据,你可以创建一个继承ContentProvider组件的内容提供器。你还应该把要放到剪贴板上的URI编码,以便它指向你想要提取的数据。另外,你必须要考虑你的应用程序的存在状态:

1. 如果你已经有了一个内容提供器,你可以给它添加功能。你可能只需要修改它的query()方法来处理粘贴数据的应用程序所输入的URI。为了处理“copy”URI的模式,也可能会修改这个方法。

2. 如果你的应用程序维护了一个内部的数据库,你可能想要把这个数据库移到内容提供器中,为从中复制数据提供方便。

3. 如果你当前没有使用数据库,你可以实现一个简单的内容提供器,它的唯一目的就是把数据提供给从剪贴板上粘贴数据的应用程序。

在内容提供器中,你至少要覆盖以下方法:

query()

粘贴数据的应用程序会假设它们可以使用你放入剪贴板中的URI来获取数据。为了支持复制数据,你应该有一个检查URI中是否包含“copy”路径的方法。然后你的应用程序再创建这个要放到剪贴板上的“copy”URI,它包含了复制路径,并指向了你想要复制的正确的记录。

getType()

这个方法应该返回一个MIME类型或者是你要复制的数据的类型。为了把MIME类型放入新的clipData对象,newUri()方法会调用getType()方法。

对于复杂数据的MIME类型在“内容提供器”专题中已经介绍了。
注意,用于复制/粘贴的内容提供器不必其他的任何方法,如insert()或update()。粘贴数据的应用程序只需要获得其所支持的MIME类型,并从你的内容提供器中复制数据。如果你已经有了这些方法,那么在复制操作中不应该引用它们。

以下代码演示了如何创建能够复制复杂数据的应用程序:

1.  在你的应用程序的全局变量中,声明基本的URI字符串和标识你要复制的数据的URI字符串的路径。还要声明要复制的数据的MIME类型:

// Declares the base URI string

privatestaticfinalString CONTACTS ="content://com.example.contacts";

 

// Declares a path string for URIs that you use to copy data

privatestaticfinalString COPY_PATH ="/copy";

 

// Declares a MIME type for the copied data

publicstaticfinalString MIME_TYPE_CONTACT ="vnd.android.cursor.item/vnd. example.contact"

2.  在用户要复制数据的Acitivity中,创建把数据复制给剪贴板的代码。在响应复制请求中,把URI放到剪贴板上:

publicclassMyCopyActivityextendsActivity{

 

    ...

 

// The user has selected a name and is requesting a copy.

case R.id.menu_copy:

 

    // Appends the last name to the base URI

    // The name is stored in "lastName"

    uriString = CONTACTS + COPY_PATH +"/"+ lastName;

 

    // Parses the string into a URI

    Uri copyUri =Uri.parse(uriString);

 

  // Gets a handle to the clipboard service.

    ClipboardManager clipboard =(ClipboardManager)

        getSystemService(Context.CLIPBOARD_SERVICE);

 

    ClipData clip =ClipData.newUri(getContentResolver(),"URI", copyUri);

 

    // Set the clipboard's primary clip.

    clipboard.setPrimaryClip(clip);

3.  在你的内容提供器的全局范围内,创建一个URI匹配器,并添加一个URI模式,该URI模式会与你放入剪贴板上的URI进行匹配:

publicclassMyCopyProviderextendsContentProvider{

 

    ...

 

// A Uri Match object that simplifies matching content URIs to patterns.

privatestaticfinalUriMatcher sURIMatcher =newUriMatcher(UriMatcher.NO_MATCH);

 

// An integer to use in switching based on the incoming URI pattern

privatestaticfinalint GET_SINGLE_CONTACT =0;

 

...

 

// Adds a matcher for the content URI. It matches

// "content://com.example.contacts/copy/*"

sUriMatcher.addURI(CONTACTS,"names/*", GET_SINGLE_CONTACT);

4.  建立一个query()方法,这个方法能够处理不同的URI模式,这取决你的编码方式,以下代码显示了剪贴板复制操作的模式:

// Sets up your provider's query() method.

publicCursor query(Uri uri,String[] projection,String selection,String[] selectionArgs,

    String sortOrder){

 

    ...

 

    // Switch based on the incoming content URI

    switch(sUriMatcher.match(uri)){

 

    case GET_SINGLE_CONTACT:

 

        // query and return the contact for the requested name. Here you would decode

        // the incoming URI, query the data model based on the last name, and return the result

        // as a Cursor.

 

    ...

 

}

5.  建立getType()方法,返回要复制的数据所对应的MIME类型:

// Sets up your provider's getType() method.

publicString getType(Uri uri){

 

    ...

 

    switch(sUriMatcher.match(uri)){

 

    case GET_SINGLE_CONTACT:

 

            return(MIME_TYPE_CONTACT);

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: