What exactly is “label” parameter in ClipData in Android?

百般思念 提交于 2019-11-30 16:46:30
ClipData clip = ClipData.newPlainText(
            "text label", 
            "content to be copied");

here text label describes what data is in clip

eg.

ClipData clip = ClipData.newPlainText(
            "user Name",
            user.getName()); 

we can retrive this by using

clip.getDescription ();

It seems like the "User-visible label for the clip data" description in the documentation should be interpreted as something you as a developer can set and then show to the user yourself and not as something that the Android system will show to the user.

When looking at the Android source code the ClipDescription.getLabel() method seems to be unused before Android 5.0. In 5.0 RemoteInput, RemoteInputCompatJellybean and com.android.mail.compose.ComposeActivity stated using the method.

If you look at the usage all these set a label that is not meant to be seen by the user but instead used to programatically identify the clip at a different place in the code.

When looking at how ClipData.newPlainText() is used within Android, most of the time null is given as label, suggesting the label is not really used for anything.

It is of course possible that some phone manufacturer or some other app developer takes the label and displays it to the user in some situation. But in general it should be safe to assume that the label of a clip will only be shown to the user in your app if you show it yourself.

Eaweb

Today while working on my app I discovered one use case for ClipData label. Some apps set it to null while other apps use it pretty much.

In the case of my app I am listening to the ClipManager's addPrimaryClipChangedListener

I am doing this in a service class that runs in the background almost all the time. I want to treat data added to the primaryClip from within my app different from data added in another app (lets say text copied in a web browser).

Here is an extract of my code and how I am using ClipData label:

mClipBoardManager.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
            @Override
            public void onPrimaryClipChanged() {
                String clipLabel = "default";
                if (mClipBoardManager.getPrimaryClip().getDescription().getLabel() != null) {
                    clipLabel = mClipBoardManager.getPrimaryClip().getDescription().getLabel().toString();
                }
                if (clipLabel.equals("auto_copy_text")) {
                    //TODO: Text from my app do stuffs you will do with text from my app
                } else {
                    //TODO: Text from some other app
                }

            }
        });

In my app when I am adding data to primaryClip I include the label like this:

private void addToClipboard(String text) {
        mClipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        mClipboardManager.setPrimaryClip(ClipData.newPlainText("auto_copy_text", text));
    }

I hope this helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!