Cách xem những văn bản đã sao chép trên android năm 2024

Trong trường hợp bạn không biết thì bộ nhớ tạm trên iPhone không thể truy cập được cho dù bạn có tìm bằng công cụ Tìm kiếm hay là mở từ Cài đặt.

Cách duy nhất để kiểm tra đó là bạn mở một ứng dụng có thể gõ văn bản như Mail, Tin nhắn, Messenger, Ghi chú,... Bạn nhấn vào chỗ trống nơi dùng để gõ văn bản cho đến khi các tùy chọn xuất hiện có nội dung như "Dán (Paste)" hoặc "Dán từ Clipboard". Chọn lệnh "Dán" để dán văn bản gần nhất mà bạn đã sao chép. Nếu không có văn bản nào được lưu trữ thì lệnh "Dán" sẽ không xuất hiện.

Cách xem những văn bản đã sao chép trên android năm 2024

Dán lên để xem nội dung bộ nhớ tạm

Để xóa văn bản mà bạn vừa sao chép được lưu trên bộ nhớ tạm, bạn cần sao chép một văn bản mới. Về cơ bản, khi sao chép một nội dung mới thì nội dung cũ trong bộ nhớ tạm sẽ bị xóa.

Bạn nhấn đè vào nơi gõ văn bản và chọn lệnh "Dán". Nếu nội dung mới xuất hiện thì nghĩa là bạn đã xóa được nội dung cũ. Nếu nội dung cũ vẫn còn thì có nghĩa là bạn đã sao chép nội dung mới thất bại. Bạn cần tìm lại nơi chứa nội dung mới và đảm bảo rằng lần này bạn đã sao chép thành công được nội dung mới.

Lưu ý: Mục 2 dành cho những ai muốn quản lý tất cả những gì được copy trên iPhone. Bạn có thể dùng ứng dụng này để coi lại, chỉnh sửa và xóa các nội dung cũ được lưu lại trên ứng dụng.

Bạn có thể tải ứng dụng Clipboard++ cho iPhone, iPad TẠI ĐÂY.

Khi bạn đã cài đặt ứng dụng này về iPhone, iPad thì khi bạn sao chép văn bản nào đó thì chúng đều được lưu trữ trong ứng dụng này.

Cách xem những văn bản đã sao chép trên android năm 2024

Các văn bản được lưu

Bạn có thể nhấn vào bất cứ văn bản nào đã được lưu trên ứng dụng, khi đoạn văn bản đó nhảy lên đầu tiên có nghĩa là nó đã được lưu vào bộ nhớ tạm của máy. Bạn chỉ cần đi đến nơi muốn dán, nhấn đè vào chọn lệnh "Dán" là được.

Một cách khác để đưa văn bản đó vào bộ nhớ tạm đó là: Nhấn đè vào văn bản > Sao chép đến bộ nhớ tạm (Copy to Clipboard).

Android cung cấp một khung toàn diện về bảng nhớ tạm cho thao tác sao chép và dán. Khung này hỗ trợ cả loại dữ liệu đơn giản và phức tạp, bao gồm cả chuỗi văn bản, cấu trúc dữ liệu phức tạp, dữ liệu văn bản, dữ liệu trong luồng tệp nhị phân và thậm chí là tài sản của ứng dụng. Dữ liệu văn bản đơn giản được lưu trữ trực tiếp trong bảng nhớ tạm, còn dữ liệu phức tạp được lưu trữ dưới dạng tệp đối chiếu mà ứng dụng dán phân giải thông qua nhà cung cấp nội dung. Việc sao chép và dán diễn ra cả trong một ứng dụng và giữa các ứng dụng cài đặt khung.

Vì một phần của khung này sử dụng các nhà cung cấp nội dung, nên chủ đề này giả định rằng nhà phát triển biết về Android Content Provider API theo mô tả trong chủ đề Nhà cung cấp nội dung.

Người dùng mong muốn nhận được phản hồi khi sao chép nội dung vào bảng nhớ tạm. Do đó, ngoài khung ứng dụng hỗ trợ thao tác sao chép và dán, Android sẽ hiển thị một giao diện người dùng mặc định cho người dùng khi sao chép trong Android 13 (API cấp 33) trở lên. Bạn nên tiếp tục cung cấp phản hồi cho người dùng theo cách thủ công khi sao chép trong Android 12L (API cấp 32) trở xuống. Vui lòng xem ở phần sau của hướng dẫn này.

Khung bảng nhớ tạm

Khi sử dụng khung bảng nhớ tạm (clipboard framework), bạn đặt dữ liệu vào một đối tượng cắt (clip), sau đó đặt đối tượng clip đó vào bảng nhớ tạm trên toàn hệ thống. Đối tượng clip có ba dạng sau:

Văn bản Chuỗi văn bản. Bạn đặt trực tiếp chuỗi vào đối tượng clip mà sau đó được đặt vào bảng nhớ tạm. Để dán chuỗi, bạn lấy đối tượng clip từ bảng nhớ tạm và sao chép chuỗi vào bộ nhớ của ứng dụng. URI Đối tượng

// Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri)

6 đại diện cho bất kỳ dạng URI nào. Việc này chủ yếu để sao chép dữ liệu phức tạp từ một nhà cung cấp nội dung. Để sao chép dữ liệu, bạn đặt một đối tượng

// Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri)

6 vào một đối tượng clip và đặt đối tượng clip đó vào bảng nhớ tạm. Để dán dữ liệu, bạn sẽ lấy đối tượng clip, lấy đối tượng

// Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri)

6, phân giải đối tượng đó thành một nguồn dữ liệu như nhà cung cấp nội dung, rồi sao chép dữ liệu từ nguồn đó vào bộ nhớ của ứng dụng. Intent Một

// Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri)

9 (ý định). Dạng đối tượng này hỗ trợ sao chép lối tắt ứng dụng. Để sao chép dữ liệu, bạn tạo một Intent, đặt vào một đối tượng clip rồi đặt đối tượng clip vào bảng nhớ tạm. Để dán dữ liệu, bạn sẽ lấy đối tượng clip rồi sao chép đối tượng Intent vào vùng bộ nhớ của ứng dụng.

Bảng nhớ tạm chỉ chứa một đối tượng clip tại mỗi thời điểm. Khi ứng dụng đặt đối tượng clip trên bảng nhớ tạm, đối tượng clip trước đó sẽ biến mất.

Nếu muốn cho phép người dùng dán dữ liệu vào ứng dụng của mình, bạn không cần phải xử lý tất cả các loại dữ liệu. Bạn có thể kiểm tra dữ liệu trên bảng nhớ tạm trước khi cho phép người dùng dán dữ liệu đó. Ngoài việc có một số loại dữ liệu nhất định, đối tượng clip còn chứa siêu dữ liệu cho bạn biết loại hoặc loại MIME nào có sẵn. Siêu dữ liệu này giúp bạn quyết định liệu ứng dụng của mình có thể làm điều gì đó hữu ích với dữ liệu trong bảng nhớ tạm hay không. Chẳng hạn, nếu có một ứng dụng chủ yếu xử lý văn bản, bạn có thể cần bỏ qua các đối tượng clip chứa URI hoặc Intent.

Bạn cũng có thể cho phép người dùng dán văn bản bất kể dạng dữ liệu nào trên bảng nhớ tạm. Để làm điều này, bạn có thể biểu diễn dữ liệu bảng nhớ tạm dưới dạng văn bản, sau đó dán văn bản này. Phần này được mô tả trong nội dung .

Các lớp bảng nhớ tạm

Phần này mô tả các lớp mà khung bảng nhớ tạm sử dụng.

ClipboardManager

Trong hệ thống Android, bảng nhớ tạm của hệ thống được biểu thị bằng lớp

// 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);

0 toàn cục. Bạn không trực tiếp tạo một thực thể của lớp này; thay vào đó, bạn phải tham chiếu đến lớp này bằng cách gọi

// 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);

1.

ClipData, ClipData.Item và ClipDescription

Để thêm dữ liệu vào bảng nhớ tạm, bạn tạo một đối tượng

// 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);

2 chứa cả phần mô tả dữ liệu và chính dữ liệu đó. Bảng nhớ tạm chỉ chứa một

// 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);

2 tại mỗi thời điểm.

// 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);

2 chứa một đối tượng

// 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);

5 và một hoặc nhiều đối tượng

// 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);

6.

Đối tượng

// 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);

5 chứa siêu dữ liệu về đối tượng clip. Đặc biệt, đối tượng này chứa một mảng các loại MIME có sẵn cho dữ liệu của đối tượng clip. Ngoài ra, trên Android 12 (API cấp độ 31) trở lên, siêu dữ liệu bao gồm thông tin về việc hay không và về . Khi bạn đặt một đối tượng clip vào bảng nhớ tạm, thông tin này sẽ có sẵn cho các ứng dụng dán. Những ứng dụng này có thể kiểm tra xem liệu có thể xử lý dữ liệu đối tượng clip hay không.

Đối tượng

// 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);

6 chứa dữ liệu văn bản, URI hoặc Intent:

Văn bản Một

// 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);

9. URI Một

// Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri)

6. Đối tượng này thường chứa một URI của nhà cung cấp nội dung, mặc dù bất kỳ URI nào cũng đều được. Ứng dụng cung cấp dữ liệu sẽ đặt URI trên bảng nhớ tạm. Các ứng dụng muốn dán dữ liệu sẽ lấy URI từ bảng nhớ tạm và dùng URI đó để truy cập nhà cung cấp nội dung (hoặc nguồn dữ liệu khác) và truy xuất dữ liệu đó. Intent Một

// Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri)

9. Loại dữ liệu này cho phép bạn sao chép lối tắt ứng dụng vào bảng nhớ tạm. Sau đó, người dùng có thể dán lối tắt vào ứng dụng để sử dụng sau.

Bạn có thể thêm nhiều đối tượng

// 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);

6 vào một clip. Việc này cho phép người dùng sao chép và dán nhiều lựa chọn dưới dạng một đối tượng clip duy nhất. Ví dụ: nếu có một tiện ích danh sách cho phép người dùng chọn nhiều mục cùng lúc, bạn có thể sao chép tất cả các mục vào bảng nhớ tạm cùng một lúc. Để thực hiện việc này, bạn tạo một

// 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);

6 riêng cho từng mục trong danh sách, sau đó thêm các đối tượng

// 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);

6 vào đối tượng

// 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);

2.

Phương thức ClipData thuận tiện

Lớp

// 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);

2 cung cấp các phương thức tĩnh thuận tiện để tạo đối tượng

// 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);

2 bằng một đối tượng

// 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);

6và đối tượng

// 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);

5 đơn giản:

// 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);

0 Trả về đối tượng

// 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);

2, trong đó mỗi đối tượng

// 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);

6 chứa một chuỗi văn bản. Nhãn của đối tượng

// 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);

5 được thiết lập thành

// 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);

4. Loại MIME duy nhất trong

// 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);

5 là

// 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);

6.

Sử dụng

// 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);

7 để tạo một đối tượng clip từ một chuỗi văn bản.

// 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);

8 Trả về đối tượng

// 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);

2, trong đó mỗi

// 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);

6 chứa một URI. Nhãn của đối tượng

// 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);

5 được thiết lập thành

// 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);

4. Nếu URI là một URI nội dung (

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

3 trả về

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

4), phương thức sử dụng

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

5 đối tượng được cung cấp trong

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

6 để truy xuất các loại MIME có sẵn từ nhà cung cấp nội dung và lưu trữ các loại đó bằng

// 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);

5. Đối với URI không phải là URI

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

4, phương thức này sẽ thiết lập loại MIME thành

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

9.

Sử dụng

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip);

0 để tạo một đối tượng clip từ một URI, đặc biệt là URI

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

4.

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip);

2 Trả về đối tượng

// 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);

2, trong đó mỗi

// 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);

6 chứa một

// Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri)

9. Nhãn của đối tượng

// 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);

5 được thiết lập thành

// 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);

4. Loại MIME được thiết lập thành

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip);

8.

Sử dụng

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip);

9 để tạo một đối tượng clip từ một đối tượng Intent.

Chuyển đổi dữ liệu bảng nhớ tạm thành văn bản

Ngay cả khi ứng dụng của bạn chỉ xử lý văn bản, bạn vẫn có thể sao chép dữ liệu không phải văn bản từ bảng nhớ tạm bằng cách chuyển đổi chúng bằng phương thức

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
00.

Phương thức này chuyển đổi dữ liệu trong

// 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);

6 thành văn bản và trả về

// 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);

9. Giá trị mà

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
00 trả về sẽ dựa trên dạng dữ liệu trong

// 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);

6:

Văn bản Nếu

// 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);

6 là văn bản (

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
06 không mang giá trị rỗng),

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
07 sẽ trả về văn bản (text). URI Nếu

// 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);

6 là một URI (

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
09 không mang giá trị rỗng),

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
07 sẽ cố gắng sử dụng URI đó làm một URI nội dung:

  • Nếu URI là một URI nội dung và nhà cung cấp có thể trả về một dòng văn bản thì ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    07 sẽ trả về một dòng văn bản.
  • Nếu URI là một URI nội dung nhưng nhà cung cấp không cung cấp một dòng văn bản, thì ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    07 sẽ trả về kết quả biểu diễn của URI. Kết quả biểu diễn giống với kết quả do ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    13 trả về.
  • Nếu URI không phải là URI nội dung, thì ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    07 sẽ trả về kết quả biểu diễn của URI. Kết quả biểu diễn giống với kết quả do ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    13 trả về. Intent Nếu

// 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);

6 là một Ý định (

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
17 không mang giá trị rỗng),

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
07 sẽ chuyển đổi Ý định đó thành một URI Ý định và trả về URI Ý định này. Kết quả biểu diễn giống với kết quả do

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
19 trả về.

Khung bảng nhớ tạm được tóm tắt trong Hình 1. Để sao chép dữ liệu, ứng dụng sẽ đặt đối tượng

// 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);

2 vào bảng nhớ tạm

// 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);

0 toàn cục.

// 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);

2 chứa một hoặc nhiều đối tượng

// 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);

6 và một đối tượng

// 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);

5. Để dán dữ liệu, ứng dụng sẽ nhận

// 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);

2, nhận loại MIME từ

// 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);

5 và nhận dữ liệu từ

// 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);

6 hoặc từ nhà cung cấp nội dung do

// 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);

6 tham chiếu đến.

Cách xem những văn bản đã sao chép trên android năm 2024

Hình 1. Khung bảng nhớ tạm của Android

Sao chép vào Bảng nhớ tạm

Như trong phần mô tả trước, để sao chép dữ liệu vào bảng nhớ tạm mà bạn tham chiếu (handle) đến đối tượng

// 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);

0 toàn cục, tạo đối tượng

// 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);

2, thêm

// 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);

5 và một hoặc nhiều đối tượng

// 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);

6 cho đối tượng này và thêm đối tượng

// 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);

2 đã hoàn chỉnh vào đối tượng

// 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);

0. Chi tiết từng bước được mô tả dưới đây:

  1. Nếu bạn đang sao chép dữ liệu bằng URI nội dung, hãy thiết lập nhà cung cấp nội dung. Ứng dụng mẫu NotePad là một ví dụ về cách sử dụng nhà cung cấp nội dung để sao chép và dán. Lớp NotePadProvider triển khai nhà cung cấp nội dung. Lớp NotePad định nghĩa contract (hợp đồng) giữa nhà cung cấp nội dung và các ứng dụng khác, bao gồm cả các loại MIME được hỗ trợ.
  2. Lấy bảng nhớ tạm thời của hệ thống:

    Kotlin

    when(menuItem.itemId) {

    ...  
    R.id.menu_copy -> { // if the user selects copy  
        // Gets a handle to the clipboard service.  
        val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager  
    }  
    
    }

    Java

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

        getSystemService(Context.CLIPBOARD_SERVICE);
  3. Sao chép dữ liệu vào đối tượng // 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); 2 mới:

    • Đối với văn bản

      Kotlin

      // Creates a new text clip to put on the clipboard val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!")

      Java

      // Creates a new text clip to put on the clipboard ClipData clip = ClipData.newPlainText("simple text", "Hello, World!");
    • Đối với URI
      Đoạn mã này khởi tạo URI bằng cách mã hoá mã nhận dạng bản ghi vào URI nội dung cho nhà cung cấp. Kỹ thuật này được đề cập chi tiết hơn trong phần :  
      
      ### Kotlin

      // Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri)

      Java

      // 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);
    • Đối với Ý định
      Đoạn mã này khởi tạo một Ý định cho một ứng dụng, sau đó đặt nó vào đối tượng clip:  
      
      ### Kotlin

      // Creates the Intent val appIntent = Intent(this, com.example.demo.myapplication::class.java) ... // Creates a clip object with the Intent in it. Its label is "Intent" and its data is // the Intent object created previously val clip: ClipData = ClipData.newIntent("Intent", appIntent)

      Java

      // 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);
  4. Đặt đối tượng clip mới vào bảng nhớ tạm:

    Kotlin

    // Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

    Java

    // Set the clipboard's primary clip. clipboard.setPrimaryClip(clip);

Cung cấp ý kiến phản hồi khi sao chép vào bảng nhớ tạm

Người dùng mong muốn phản hồi bằng hình ảnh khi ứng dụng sao chép nội dung vào bảng nhớ tạm. Việc này được thực hiện tự động cho người dùng Android 13 trở lên, nhưng lại triển khai theo cách thủ công trong các phiên bản trước.

Kể từ Android 13, hệ thống sẽ hiển thị một thông báo xác nhận bằng hình ảnh tiêu chuẩn khi nội dung được thêm vào bảng nhớ tạm. Xác nhận mới thực hiện những việc sau:

  • Xác nhận nội dung đã được sao chép thành công.
  • Cung cấp bản xem trước nội dung đã sao chép.

Trên Android 12L (API cấp 32) trở xuống, người dùng thường không chắc chắn thời điểm họ đã sao chép thành công nội dung hoặc những gì họ đã sao chép. Tính năng này chuẩn hoá các thông báo do ứng dụng hiển thị sau khi sao chép, đồng thời cung cấp cho người dùng nhiều quyền kiểm soát hơn đối với bảng nhớ tạm của họ.

Cách xem những văn bản đã sao chép trên android năm 2024
Giao diện người dùng hiển thị khi nội dung nhập vào bảng nhớ tạm trong Android 13 trở lên.

Tránh thông báo trùng lặp

Trên Android 12L (API cấp 32) trở xuống, bạn nên thông báo cho người dùng về việc họ đã sao chép thành công bằng cách phát hành một tiện ích bật lên hình ảnh trong ứng dụng (chẳng hạn như các Thông báo ngắn hoặc Thanh thông báo nhanh) sau khi sao chép.

Để tránh hiển thị thông tin trùng lặp, bạn nên xoá mọi tiện ích bật lên xuất hiện sau bản sao trong ứng dụng dành cho Android 13 trở lên.

Cách xem những văn bản đã sao chép trên android năm 2024
Nếu bạn hiển thị một thanh thông báo nhanh xác nhận sao chép trong Android 13, thì người dùng sẽ thấy các thông báo trùng lặp.
Cách xem những văn bản đã sao chép trên android năm 2024
Nếu bạn hiển thị một thông báo ngắn xác nhận sao chép trong Android 13, thì người dùng sẽ thấy các thông báo trùng lặp.

Sau đây là một ví dụ về cách triển khai phương thức này:

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
0

Thêm nội dung nhạy cảm vào bảng nhớ tạm

Nếu ứng dụng cho phép người dùng sao chép nội dung nhạy cảm, chẳng hạn như mật khẩu hoặc thông tin thẻ tín dụng vào bảng nhớ tạm, thì bạn phải thêm cờ vào

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
36 của ClipData trước khi gọi

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
37. Việc thêm cờ này sẽ ngăn nội dung nhạy cảm xuất hiện trong bất kỳ bản xem trước nội dung nào có trong thông báo xác nhận bằng hình ảnh sao chép trong Android 13 trở lên.

Cách xem những văn bản đã sao chép trên android năm 2024
Bản xem trước văn bản sao chép không gắn cờ nội dung nhạy cảm.
Cách xem những văn bản đã sao chép trên android năm 2024
Bản xem trước văn bản sao chép có gắn cờ nội dung nhạy cảm.

Để gắn cờ nội dung nhạy cảm, hãy thêm một boolean bổ sung vào

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
36. Mọi ứng dụng đều nên làm việc này, bất kể cấp độ API mục tiêu nào.

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
1

Dán từ Bảng nhớ tạm

Như đã mô tả trước đó, bạn dán dữ liệu từ bảng nhớ tạm bằng cách lấy đối tượng bảng nhớ tạm toàn cục, lấy đối tượng clip, xem dữ liệu của đối tượng và nếu có thể, sao chép dữ liệu từ đối tượng clip vào bộ nhớ của riêng bạn. Phần này giải thích chi tiết cách dán 3 loại dữ liệu trên bảng nhớ tạm.

Dán văn bản thuần tuý

Để dán văn bản thuần tuý, trước tiên hãy lấy bảng nhớ tạm toàn cục và xác minh rằng bảng nhớ tạm có thể trả về văn bản thuần tuý. Sau đó, lấy đối tượng clip và sao chép văn bản của đối tượng đó vào bộ nhớ của riêng bạn bằng

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
06 theo cách như sau:

  1. Lấy đối tượng

    // 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); 0 toàn cục bằng // 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); 1. Ngoài ra, hãy khai báo một biến toàn cục để chứa văn bản được dán:

    Kotlin

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    2

    Java

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    3
  2. Tiếp theo, hãy xác định xem bạn nên bật hay tắt tuỳ chọn "dán" trong Hoạt động (activity) hiện tại. Bạn nên xác minh xem bảng nhớ tạm có chứa một đối tượng clip và bạn có thể xử lý loại dữ liệu do đối tượng clip đó biểu diễn hay không:

    Kotlin

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    4

    Java

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    5
  3. Sao chép dữ liệu từ bảng nhớ tạm. Bạn chỉ có thể thực hiện đến bước này nếu mục "dán" trong trình đơn được bật. Vì vậy bạn có thể giả định rằng bảng nhớ tạm hiện chứa văn bản thuần tuý. Bạn chưa biết liệu bảng nhớ tạm có chứa một chuỗi văn bản hoặc một URI trỏ đến văn bản thuần tuý hay không. Đoạn mã sau đây giúp kiểm thử điều này nhưng chỉ bao gồm mã để xử lý văn bản thuần tuý:

    Kotlin

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    6

    Java

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    7

Dán dữ liệu từ URI nội dung

Nếu đối tượng

// 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);

6 chứa một URI nội dung và bạn đã xác định rằng có thể xử lý một trong các loại MIME của đối tượng đó, hãy tạo một

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

5, sau đó gọi phương thức nhà cung cấp nội dung thích hợp để truy xuất dữ liệu.

Quy trình sau đây mô tả cách lấy dữ liệu từ nhà cung cấp nội dung dựa trên URI nội dung trên bảng nhớ tạm. Quy trình này kiểm tra xem nhà cung cấp có cung cấp loại MIME mà ứng dụng có thể sử dụng hay không:

  1. Khai báo biến toàn cục chứa loại MIME:

    Kotlin

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    8

    Java

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    9
  2. Lấy bảng nhớ tạm toàn cục. Ngoài ra, hãy sử dụng trình phân giải nội dung để bạn có thể truy cập vào nhà cung cấp nội dung:

    Kotlin

    // Creates a new text clip to put on the clipboard val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!") 0

    Java

    // Creates a new text clip to put on the clipboard val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!") 1
  3. Lấy đối tượng clip chính từ bảng nhớ tạm và nội dung của nó dưới dạng URI:

    Kotlin

    // Creates a new text clip to put on the clipboard val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!") 2

    Java

    // Creates a new text clip to put on the clipboard val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!") 3
  4. Kiểm tra xem URI đó có phải là URI nội dung hay không bằng cách gọi

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    44. Phương thức này trả về giá trị rỗng nếu ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    45 không trỏ đến một nhà cung cấp nội dung hợp lệ:

    Kotlin

    // Creates a new text clip to put on the clipboard val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!") 4

    Java

    // Creates a new text clip to put on the clipboard val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!") 5
  5. Kiểm tra xem nhà cung cấp nội dung có hỗ trợ loại MIME mà ứng dụng hiện tại hiểu được hay không. Nếu có, gọi

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    46 để lấy dữ liệu. Giá trị trả về là ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    47:

    Kotlin

    // Creates a new text clip to put on the clipboard val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!") 6

    Java

    // Creates a new text clip to put on the clipboard val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!") 7

Dán Ý định

Để dán một Ý định, trước tiên hãy lấy bảng nhớ tạm toàn cục. Kiểm tra đối tượng

// 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);

6 để xem đối tượng đó có chứa Ý định hay không. Sau đó, gọi

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
17 để sao chép Ý định vào bộ nhớ của riêng bạn. Đoạn mã sau hướng dẫn cách thực hiện:

Kotlin

// Creates a new text clip to put on the clipboard val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!")

8

Java

// Creates a new text clip to put on the clipboard val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!")

9

Hệ thống hiển thị thông báo khi ứng dụng truy cập vào dữ liệu bảng nhớ tạm

Trên Android 12 (API cấp 31) trở lên, hệ thống thường hiển thị một thông báo nhanh bằng thông báo khi ứng dụng của bạn gọi . Văn bản bên trong thông báo có định dạng sau:

// Creates a new text clip to put on the clipboard ClipData clip = ClipData.newPlainText("simple text", "Hello, World!");

0

Hệ thống không hiển thị thông báo nhanh khi ứng dụng của bạn:

  • Truy cập vào ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    51 qua ứng dụng của bạn.
  • Liên tục truy cập vào ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    51 từ một ứng dụng cụ thể. Thông báo nhanh chỉ xuất hiện khi ứng dụng của bạn truy cập dữ liệu của ứng dụng đó lần đầu tiên.
  • Truy xuất siêu dữ liệu cho đối tượng clip, chẳng hạn như bằng cách gọi thay vì ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    50.

Sử dụng nhà cung cấp nội dung để sao chép dữ liệu phức tạp

Nhà cung cấp nội dung hỗ trợ sao chép dữ liệu phức tạp như bản ghi cơ sở dữ liệu hoặc dòng tệp. Để sao chép dữ liệu, đặt một URI nội dung vào bảng nhớ tạm. Sau đó, các ứng dụng dán lấy URI này từ bảng nhớ tạm và dùng URI đó để truy xuất dữ liệu trong cơ sở dữ liệu hoặc trình mô tả dòng tệp.

Vì ứng dụng dán chỉ có URI nội dung cho dữ liệu nên ứng dụng này cần biết dữ liệu nào cần truy xuất. Bạn có thể cung cấp thông tin này bằng cách mã hoá một giá trị nhận dạng cho dữ liệu trên chính URI, hoặc bạn có thể cung cấp một URI độc nhất để trả về dữ liệu mà bạn muốn sao chép. Kỹ thuật bạn chọn phụ thuộc vào cách sắp xếp dữ liệu của bạn.

Các phần sau đây mô tả cách thiết lập URI, cách cung cấp dữ liệu phức tạp và cách cung cấp dòng tệp. Những nội dung sau đây giả định rằng bạn đã nắm rõ các nguyên tắc chung về thiết kế nhà cung cấp nội dung.

Mã hoá giá trị nhận dạng trên URI

Một kỹ thuật hữu ích để sao chép dữ liệu vào bảng nhớ tạm bằng URI là mã hoá giá trị nhận dạng cho chính dữ liệu đó trong URI. Nhà cung cấp nội dung của bạn có thể lấy giá trị nhận dạng từ URI và dùng giá trị đó để truy xuất dữ liệu. Ứng dụng dán không cần phải biết rằng có giá trị nhận dạng hay không mà chỉ cần lấy "tệp tham chiếu" (URI cộng với giá trị nhận dạng) từ bảng nhớ tạm, cung cấp tệp đó cho nhà cung cấp nội dung của bạn rồi tiến hành lấy lại dữ liệu.

Bạn thường mã hoá giá trị nhận dạng trên một URI nội dung bằng cách ghép nối giá trị nhận dạng đó vào cuối URI. Ví dụ: giả sử bạn định nghĩa URI nhà cung cấp như chuỗi sau:

// Creates a new text clip to put on the clipboard ClipData clip = ClipData.newPlainText("simple text", "Hello, World!");

1

Nếu muốn mã hoá tên vào URI này, bạn sẽ sử dụng đoạn mã sau:

Kotlin

// Creates a new text clip to put on the clipboard ClipData clip = ClipData.newPlainText("simple text", "Hello, World!");

2

Java

// Creates a new text clip to put on the clipboard ClipData clip = ClipData.newPlainText("simple text", "Hello, World!");

3

Nếu đang sử dụng một nhà cung cấp nội dung, bạn nên thêm một đường dẫn URI mới cho biết URI đó là để sao chép. Ví dụ: giả sử bạn đã có đường dẫn URI sau:

// Creates a new text clip to put on the clipboard ClipData clip = ClipData.newPlainText("simple text", "Hello, World!");

4

Bạn có thể thêm một đường dẫn khác dành riêng cho việc sao chép URI:

// Creates a new text clip to put on the clipboard ClipData clip = ClipData.newPlainText("simple text", "Hello, World!");

5

Sau đó, bạn có thể phát hiện URI "sao chép" đó bằng cách so khớp mẫu và xử lý URI bằng mã riêng cho việc sao chép và dán.

Bạn thường sử dụng kỹ thuật mã hoá nếu đã sử dụng nhà cung cấp nội dung, cơ sở dữ liệu nội bộ hoặc bảng nội bộ để sắp xếp dữ liệu. Trong những trường hợp này, bạn có nhiều dữ liệu muốn sao chép và có thể bạn có một giá trị nhận dạng độc nhất cho từng dữ liệu. Để phản hồi truy vấn từ ứng dụng dán, bạn có thể tra cứu dữ liệu theo giá trị nhận dạng và trả về dữ liệu đó.

Nếu bạn không có nhiều dữ liệu thì có thể không cần phải mã hoá giá trị nhận dạng. Bạn chỉ cần sử dụng một URI độc nhất dành riêng cho nhà cung cấp của mình. Để phản hồi một truy vấn, nhà cung cấp của bạn sẽ trả về dữ liệu hiện có.

Lấy một bản ghi theo ID được dùng trong ứng dụng mẫu Note Pad để mở ghi chú trong danh sách ghi chú. Mẫu này sử dụng trường

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
55 từ cơ sở dữ liệu SQL, nhưng bạn có thể có bất kỳ giá trị nhận dạng kiểu số hoặc ký tự nào.

Sao chép cấu trúc dữ liệu

Bạn thiết lập một nhà cung cấp nội dung để sao chép và dán dữ liệu phức tạp làm một lớp con của thành phần

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
56. Bạn cũng nên mã hoá URI mà bạn đặt vào bảng nhớ tạm để URL đó trỏ đến đúng bản ghi bạn muốn cung cấp. Ngoài ra, bạn phải xem xét trạng thái hiện tại của ứng dụng:

  • Nếu đã có nhà cung cấp nội dung, bạn có thể thêm vào phần chức năng của nhà cung cấp đó. Bạn chỉ có thể cần chỉnh sửa phương thức ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    57 để xử lý các URI đến từ các ứng dụng muốn dán dữ liệu. Bạn nên chỉnh sửa phương thức để xử lý mẫu URI "sao chép".
  • Nếu ứng dụng của bạn duy trì một cơ sở dữ liệu nội bộ, bạn nên di chuyển cơ sở dữ liệu này vào một nhà cung cấp nội dung để dễ dàng sao chép từ nhà cung cấp đó.
  • Nếu không sử dụng cơ sở dữ liệu, bạn có thể triển khai một nhà cung cấp nội dung đơn giản với mục đích duy nhất là cung cấp dữ liệu cho các ứng dụng mà dán dữ liệu từ bảng nhớ tạm.

Trong nhà cung cấp nội dung, ít nhất bạn sẽ cần ghi đè các phương thức sau:

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
57 Các ứng dụng dán sẽ cho rằng các ứng dụng đó có thể lấy dữ liệu của bạn bằng cách sử dụng phương thức này qua URI mà bạn đặt trên bảng nhớ tạm. Để hỗ trợ sao chép, bạn nên dùng phương thức này để phát hiện các URI chứa đường dẫn "sao chép" đặc biệt. Sau đó, ứng dụng của bạn có thể tạo URI "sao chép" để đưa vào bảng nhớ tạm chứa đường dẫn sao chép và con trỏ đến bản ghi chính xác mà bạn muốn sao chép.

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
59 Phương thức này sẽ trả về loại MIME hoặc các loại MIME cho dữ liệu mà bạn định sao chép. Phương thức

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip);

0 gọi

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
59 để đặt các loại MIME vào đối tượng

// 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);

2 mới.

Các loại MIME cho dữ liệu phức tạp được mô tả trong phầnNhà cung cấp nội dung.

Lưu ý rằng bạn không nhất thiết phải có bất kỳ phương thức nào khác dành cho nhà cung cấp nội dung, chẳng hạn như

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
63 hoặc

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
64. Một ứng dụng dán chỉ cần lấy các loại MIME được hỗ trợ và sao chép dữ liệu từ nhà cung cấp của bạn. Nếu bạn đã sử dụng các phương thức này, chúng sẽ không ảnh hưởng đến hoạt động sao chép.

Các đoạn mã sau hướng dẫn cách thiết lập ứng dụng của bạn để sao chép dữ liệu phức tạp:

  1. Trong các hằng số toàn cục của ứng dụng, khai báo chuỗi URI cơ sở và đường dẫn xác định chuỗi URI mà bạn sử dụng để sao chép dữ liệu. Ngoài ra, hãy khai báo loại MIME cho dữ liệu được sao chép:

    Kotlin

    // Creates a new text clip to put on the clipboard ClipData clip = ClipData.newPlainText("simple text", "Hello, World!"); 6

    Java

    // Creates a new text clip to put on the clipboard ClipData clip = ClipData.newPlainText("simple text", "Hello, World!"); 7
  2. Trong Hoạt động mà người dùng sao chép dữ liệu, hãy thiết lập mã để sao chép dữ liệu vào bảng nhớ tạm. Để phản hồi lại một yêu cầu sao chép, đặt URI vào bảng nhớ tạm:

    Kotlin

    // Creates a new text clip to put on the clipboard ClipData clip = ClipData.newPlainText("simple text", "Hello, World!"); 8

    Java

    // Creates a new text clip to put on the clipboard ClipData clip = ClipData.newPlainText("simple text", "Hello, World!"); 9
  3. Trong tầm vực toàn cục của nhà cung cấp nội dung, tạo một trình so khớp URI và thêm một mẫu URI sẽ khớp với các URI mà bạn đặt trên bảng nhớ tạm:

    Kotlin

    // Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri) 0

    Java

    // Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri) 1
  4. Thiết lập phương thức

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    65. Phương thức này có thể xử lý các mẫu URI khác nhau, tuỳ thuộc vào cách bạn mã hoá, nhưng chỉ mẫu dùng cho thao tác sao chép vào bảng nhớ tạm mới được hiển thị:

    Kotlin

    // Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri) 2

    Java

    // Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri) 3
  5. Thiết lập phương thức

    ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    59 để trả về một loại MIME thích hợp cho dữ liệu được sao chép:

    Kotlin

    // Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri) 4

    Java

    // Creates a Uri based on a base Uri and a record ID based on the contact's last name // Declares the base URI string const val CONTACTS = "content://com.example.contacts" // Declares a path string for URIs that you use to copy data const val COPY_PATH = "/copy" // Declares the Uri to paste to the clipboard val copyUri: Uri = 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. val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri) 5

Phần mô tả cách lấy URI nội dung từ bảng nhớ tạm và sử dụng URI đó để lấy và dán dữ liệu.

Sao chép luồng dữ liệu

Bạn có thể sao chép và dán lượng lớn văn bản và dữ liệu tệp nhị phân dưới dạng luồng. Dữ liệu có thể có các dạng như sau:

  • Tệp được lưu trữ trên thiết bị thực tế.
  • Luồng từ socket.
  • Lượng dữ liệu lớn được lưu trữ trong hệ thống cơ sở dữ liệu cơ bản của nhà cung cấp.

Nhà cung cấp nội dung cho luồng dữ liệu sẽ cấp quyền truy cập dữ liệu bằng đối tượng chỉ số mô tả tệp, chẳng hạn như

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
67 thay vì đối tượng

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
47. Ứng dụng dán sẽ đọc luồng dữ liệu bằng chỉ số mô tả tệp này.

Để thiết lập ứng dụng của bạn cho việc sao chép luồng dữ liệu với nhà cung cấp, hãy làm theo các bước sau:

  1. Thiết lập URI nội dung cho luồng dữ liệu mà bạn đang đặt vào bảng nhớ tạm. Các phương án để thực hiện việc này bao gồm:
    • Mã hoá giá trị nhận dạng cho luồng dữ liệu vào URI theo cách được mô tả trong phần , sau đó duy trì bảng trong nhà cung cấp chứa giá trị nhận dạng và tên luồng tương ứng.
    • Mã hoá tên luồng trực tiếp trên URI.
    • Sử dụng một URI độc nhất luôn trả về luồng hiện tại từ nhà cung cấp. Nếu sử dụng phương án này, bạn phải nhớ cập nhật nhà cung cấp của mình để trỏ đến một luồng khác bất cứ khi nào bạn sao chép luồng vào bảng nhớ tạm qua URI.
  2. Cung cấp một loại MIME cho mỗi loại luồng dữ liệu mà bạn định cung cấp. Các ứng dụng dán cần thông tin này để xác định xem có thể dán dữ liệu vào bảng nhớ tạm hay không.
  3. Triển khai một trong các phương thức ... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);  
    
    56 mà trả về chỉ số mô tả tệp cho luồng. Nếu bạn mã hoá giá trị nhận dạng trên URI nội dung, sử dụng phương thức này để xác định luồng nào cần mở.
  4. Để sao chép luồng dữ liệu vào bảng nhớ tạm, khởi tạo URI nội dung và đặt URI đó vào bảng nhớ tạm.

Để dán luồng dữ liệu, ứng dụng sẽ lấy đối tượng clip từ bảng nhớ tạm và URI, sau đó sử dụng URI đó trong lệnh gọi phương thức chỉ số mô tả tệp

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

5 mà mở luồng. Phương thức

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

5 gọi phương thức

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
56 tương ứng và truyền URI nội dung vào phương thức tương ứng đó. Nhà cung cấp của bạn trả về chỉ số mô tả tệp cho phương thức

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

5. Sau đó, ứng dụng dán có trách nhiệm đọc dữ liệu từ luồng.

Danh sách sau đây liệt kê các phương thức chỉ số mô tả tệp quan trọng nhất đối với nhà cung cấp nội dung. Mỗi phương thức đều có một phương thức

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

5 tương ứng, trong đó có chuỗi "Mô tả" sẽ được thêm vào tên phương thức; ví dụ: phương thức tương tự

// Set the clipboard's primary clip. clipboard.setPrimaryClip(clip)

5 của

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
76 là

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
77:

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
78 Phương thức này sẽ trả về chỉ số mô tả tệp tài sản, nhưng chỉ khi nhà cung cấp hỗ trợ loại MIME đã cung cấp. Phương thức gọi (ứng dụng thực hiện thao tác dán) cung cấp một mẫu loại MIME. Nhà cung cấp nội dung (của ứng dụng đã sao chép URI vào bảng nhớ tạm) trả về một tham chiếu tệp (file handle)

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
67 nếu có thể cung cấp loại MIME đó hoặc báo ngoại lệ nếu không thể.

Phương thức này xử lý các mục con trong tệp. Bạn có thể sử dụng phương thức này để đọc các tài sản mà nhà cung cấp nội dung đã sao chép vào bảng nhớ tạm.

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
76 Phương thức này là một phương thức chung của

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
78. Phương thức này không lọc các loại MIME được phép nhưng có thể đọc các mục con trong tệp.

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
82 Phương thức này là một phương thức chung của

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
76. Phương thức này không thể đọc các mục con trong tệp.

Bạn có thể chọn sử dụng phương thức

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
84 cùng với phương thức chỉ số mô tả tệp. Cách này cho phép ứng dụng dán đọc chuỗi dữ liệu trong luồng trong luồng nền (background thread) thông qua một ống dẫn (pipe). Để sử dụng phương thức này, bạn cần triển khai giao diện

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
85. Ví dụ về cách sử dụng phương thức này được mô tả trong phần ứng dụng mẫu Note Pad, trong phương thức

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
86 của

... // if the user selects copy case R.id.menu_copy: // Gets a handle to the clipboard service. ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);
87.