- Android 系统概述
- 什么是智能手机?智能手机操作系统有哪些?
- Android(安卓)是什么?
- Android 系统架构(图解)
- Android 7 新特性介绍
- Android 开发环境搭建
- Android 开发环境搭建教程(图解)
- Android SDK 是什么?
- 通过 Android Studio 创建 Android 应用程序(附带解析)
- Android Studio 的基本调试
- Android 应用程序结构:Android 组件和资源详解
- Android 四大核心组件:Activity+Service+BroadcastReceiver+ContentProvider
- Android Activity 组件及其生命周期
- Android 资源(res 和 assets 目录)
- AndroidManifest.xml 配置文件详解
- Android App Widgets 组件详解
- Android 系统的进程和线程
- Android GUI(图形用户界面)开发
- Android View 和 ViewGroup
- Android 使用 XML 文件定义用户界面
- Android 五大布局:FrameLayout、LinearLayout、AbsoluteLayout、RelativeLayout 和 TableLayout
- Android 创建 Widget 组件实例详解
- Android Button:按钮控件
- Android CheckBox:多选按钮控件
- Android RadioGroup:单选按钮控件
- Android TextView:文本框控件
- Android EditText:编辑框控件
- Android Spinner:下拉列表控件
- Android AutoCompleteTextView:自动完成文本框控件
- Android DatePicker 和 TimePicker:时间日期选择器
- Android ProgressBar:进度条控件
- Android ScrollView:滚动视图
- Android SeekBar:拖动条控件
- Android RatingBar:评价条控件
- Android ImageView 和 ImageButton:图片视图和图片按钮
- Android ImageSwitcher 和 Gallery:图片切换器和图库
- Android GridView:网格视图
- Android Tab:面板标签控件
- Android 菜单:选项菜单+上下文菜单+子菜单
- Android Bitmap(点阵图像、绘制图像)
- Android 对话框(AlertDialog+ProgressDialog)
- Android 多窗口模式(分屏模式)
- Android 事件监听器和回调方法
- Android 电话短信拍照开发教程
- Android Intent:不同应用程序之间通信的桥梁
- Android 实现拨打电话功能
- Android 实现接收短信和发送短信功能
- Android 调用相机进行拍照
- Android 多媒体开发
- Android Service 生命周期和使用方法
- Android BroadcastReceiver:接收广播
- Android 音频以及音频播放器开发实例
- Android MediaRecorder 录制音频
- Android 视频以及视频播放器实例
- Android Camera 相机以及相机程序开发实例
- Android MediaRecorder 录制视频
- Android 数据存储(SQLite 数据库、Android App 数据备份和恢复 )
- Android 数据存储之 SharedPreferences
- Android 数据存储之文件存储
- Android 使用 SQLite 数据库存数数据
- Android SQLite 数据库操作实例
- Android ContentProvider(内容提供者)
- Android 使用系统提供的 ContentProvider
- Android 自定义 ContentProvider
- Android 使用自定义 ContentProvider
- Google App Engine 是什么?
- Android APP 数据备份与恢复
- Android BackupAgent 实现数据备份与恢复
- Android BackupAgentHelper 实现数据备份与恢复
- Android 网络编程
- 借助 GPE 让 Android APP 和 Google App Engine 通信
- Android HTTP 通信
- Android HttpURLConnection 访问互联网资源
- Android 通过 GET 方式获取互联网资源
- Android 通过 POST 方式获取互联网资源
- Android Socket 编程(附带实例)
- Bluetooth(蓝牙)是什么?
- Android 蓝牙通信开发教程(详解版)
- Android 蓝牙通信开发实例演示
- WIFI(无线局域网)是什么?
- Android WiFi 开发(WiFi 编程)实例演示
- WIFI Direct(WIFI 直连)是什么?
- Android WIFI Direct 开发教程
- Android WIFI Direct 开发实例演示
- NFC(近场通信)是什么?
- Android NFC 开发教程
- Android USB 开发简介
- Android USB 附件模式开发教程
- Android USB 主机模式开发教程
- Android SIP 开发教程
- Android 智能传感器
- LBS(位置服务)和 GPS(全球定位系统)简介
- Android LBS 位置服务开发简介
- Android GPS 定位开发教程
- Google Map API 开发简介
- Android Google Map API Key 详细申请步骤
- Google Map API 开发 Android 地图应用实例
- Android 传感器开发教程
- Android 运动传感器开发教程
- Android 位置传感器开发教程
- Android 环境传感器开发教程
- Android 绘图(2D 绘图、3D 绘图)
- Android 2D 绘图开发简介
- Android 使用自定义 View 绘图
- Android 使用 Bitmap 绘图
- Android 使用 SurfaceView 绘制静态图像
- Android 使用 SurfaceView 绘制动态图像
- Android Drawable 开发简介
- Android 3D 绘图开发简介
- Android 硬件加速开发简介
- Android RenderScript 开发简介
- Android App 的国际化和本地化
- Android 国际化和本地化开发简介
- Android 手机区域设置详细步骤
- Android 未本地化应用开发教程
- Android 本地化应用开发教程
- Android 文本输入与复制粘贴
- Android 剪贴板框架开发简介
- Android 剪贴板数据转文本开发简介
- Android 复制粘贴开发教程
- Android Content Provider 复制复杂数据开发教程
- Android 一键复制粘贴开发实例
- Android 企业级应用开发
- Android 设备管理 API 概述
- Android 开发设备管理 API 应用教程
- Android 文本语音 API 开发教程
- Android TV 应用开发教程
- Android TV 应用开发实例
- Android 可穿戴设备应用开发教程
- Android 应用程序的发布
- Android 应用程序发布的步骤
- Android 为什么要为应用程序签名?
- Android 导出未签名应用程序开发步骤
- Android 生成签名文件开发教程
- Android 应用程序签名开发教程
- zipalign 工具优化应用程序开发简介
- Google Play Store( Android Market)发布简介
Android 复制粘贴开发教程
前面《 Android 剪贴板框架简介 》和《 Android 剪贴板数据转文本简介 》教程我们讲了如果要把数据复制到剪贴板(剪贴板句柄指向全局的 ClipboardManager 对象)上,需要创建一个 ClipData 对象,再把一个 ClipDescription 和一个以上的 ClipData.Item 对象加入其中,最后把这个 ClipData 添加到 ClipboardManager 对象中去。
而要从剪贴板粘贴数据,需先获得全局剪贴板对象,再获取 clip 对象,查找其中的数据,最后从 clip 对象中把数据复制到自己的存储中。
本节将详细描述把数据复制到剪切板的具体实现过程和如何针对三种剪贴板数据格式进行从剪切板粘贴数据。
复制
具体的实现过程如下:
要复制 Content URI 类型的数据,先要建立一个 Content Provider。
NotePad 是一个使用 Content Provider 复制粘贴数据的示例。NotePadProvider 类实现了 ContentProvider。NotePad 类定义了该 Provider 和其他应用程序的交互方式,包括支持所用的 MIME 类型。
1)获取系统剪贴板。
其代码如下:
... //if the user selects copy case R.id.menu_copy: //Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPNOARD_SERVICE);
2)复制数据给一个 ClipData 对象。
①文本
如果数据是一个文本,代码如下:
// Creates a new text clip to put on the clipboard
ClipData clip = ClipData.newPlainText("simple text","Hello, World!");
② URI
如果数据是一个 URI 对象,把记录 ID 编入 Provider 用到的 Content URI 中的代码如下:
// Creates a Uri based on a base Uri and a record 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 URIs that you use to copy data private static final String COPY_PATH = "/copy"; // Declares the Uri to paste to the clipboard Uri copyUri = Uri.parse(CONTACTS + COPY_PATH + "/" + lastName); ... // Creates a new URI clip object. The system uses the anonymous getContentResolver() object to // get MIME types from provider. The clip object's label is "URI", and its data is // the Uri previously created. ClipData clip = ClipData.newUri(getContentResolver(),"URI",copyUri);
③ Intent
如果数据是一个 Intent 对象,将其放入 clip 对象中的代码如下:
//Creates the Intent Intent appIntent = new Intent(this,com.example.demo.myapplication.class); ... //Creates a clip object with the Intent in it.Its label is "Intent" and its data is //the Intent object created previously ClipData clip = ClipData.newIntent("Intent",appIntent);
把新建的 clip 对象放入剪贴板的代码如下:
// Set the clipboard's primary clip.
clipboard.setPrimaryClip(clip);
粘贴
1.粘贴普通文本
要粘贴普通文本,首先获得全局剪贴板,并确认能否返回普通文本,然后获取 clip 对象,用 getText() 把其中的文本复制到自己的存储中。实现步骤如下:
1) 用 getSystemService(CLIPBOARD_SERVICE) 获得全局 ClipboardManager 对象,并声明一个全局变量,用来存放粘贴到的文本。
ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
String pasteData="";
2) 确定启用或禁用当前 Activity 的“粘贴”选项,并验证剪贴板中是否包含 clip,且程序是否有能力处理其数据类型。
// Gets the ID of the "paste" menu item MenuItem mPasteItem = menu.findItem(R.id.menu_paste); // If the clipboard doesn't contain data, disable the paste menu item. // If it does contain data, decide if you can handle the data. if (!(clipboard.hasPrimaryClip())) { mPasteItem.setEnabled(false); } else if (!(clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))) { // This disables the paste menu item, since the clipboard has data but it is not plain text mPasteItem.setEnabled(false); } else { // This enables the paste menu item, since the clipboard contains plain text. mPasteltem.setEnabled(true); }
3) 从剪贴板中复制数据。只有“粘贴”菜单项启用时,程序才会运行至此,所以这时可以假定剪贴板已经包含普通文本,不过还不清楚里面包含文本字符串还是指向普通文本的 URI。测试处理普通文本的部分代码如下:
// Responds to the user selecting "paste" case R.id.menu_paste: // Examines the item on the clipboard. If getTextO does not return null, the clip item contains the // text. Assumes that this application can only handle one item at a time. ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0); // Gets the clipboard as text. pasteData = item.getText(); // If the string contains data, then the paste operation is done if (pasteData != null) { return; // The clipboard does not contain text. If it contains a URI, attempts to get data from it } else { Uri pasteUri = item.getUri(); // If the URI contains something, try to get text from it if (pasteUri != null) { // calls a routine to resolve the URI and get data from it. This routine is not // presented here. pasteData = resolveUri(Uri); return; } else { // Something is wrong. The MIME type was plain text, but the clipboard does not contain either // text or a Uri. Report an error. Log.e("Clipboard contains an invalid data type"); return; } }
2.从 Content URI 粘贴数据
如果 ClipData.Item 对象包含一个 Content URI,程序也能处理其中的 MIME 类型,可创建一个 ContentResolver 对象,并调用 Content Provider 的相关方法来获取数据。
以下过程描述了如何根据剪贴板中的 Content URI 从 Content Provider 获取数据。
1)声明全局变量,用于存放 MIME 类型。
程序先检查 MIME 类型,确认能够使用 Provider 提供的数据。
// Declares a MIME type constant to match against the MIME types offered by the provider
public static final String MIME_TYPE_CONTACT = "vnd.android.cursor.item/vnd.example.contact"
2)获取全局剪贴板,创建一个用于访问 Content Provider 的 Content Resolver 对象。
//Gets a handle to the Clipboard Manager ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); //Gets a content resolver instance ContentResolver cr = getContentResolver();
3)从剪贴板获取主 clip,并把内容解析为 URI。
// Gets the clipboard data from the clipboard ClipData clip = clipboard.getPrimaryClip(); if (clip != null) { // Gets the first item from the clipboard data ClipData.Item item = clip.getltemAt(0); // Tries to get the item's contents as a URI Uri pasteUri = item.getUri();
4)通过调用 getType(Uri),判断 URI 是否为 Content URI。如果 URI 未指向合法的 Content Provider,该方法返回 null。
//If the clipboard contains a URI reference if(pasteUri != null){ Is this a content URI? String uriMimeType = cr.getType(pasteUri); }
5)判断 Content Provider 是否支持应用程序识别的 MIME 类型。若支持,则调用 ContentResolver.query() 来获取数据,返回值是一个 Cursor 对象。
// 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)) { // Get the data from the content provider. Cursor pasteCursor = cr.query(uri, null, null, null, null); // If the Cursor contains data, move to the first record if (pasteCursor != null) { if (pasteCursor.moveToFirst()) { // get the data from the Cursor here. The code will vary according to the // format of the data model. } } // close the Cursor pasteCursor.close(); } } }
3.粘贴 Intent
要粘贴一个 Intent,首先获取全局剪贴板,再检查 ClipData.Item 对象是否包含 Intent。最后调用 getIntent() 把 Intent 复制到程序的存储中。
实现代码如下:
// Gets a handle to the Clipboard Manager ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); // Checks to see if the clip item contains an Intent, by testing to see if getlntentO returns null Intent pasteIntent = clipboard.getPriraaryClip().getItemAt(0).getIntent(); if (pasteIntent != null) { // handle the Intent } else { // ignore the clipboard, or issue an error if your application was expecting an Intent to be // on the clipboard }
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论