Globally register custom TextClassifier with TextClassificationManager (Android O)

Viewed 662

Starting with Android 8.0 (Oreo) / API level 26, Android shows contextual actions for highlighted text (e.g. "Call" action if a phone number is selected). Also, Android automatically selects multiple words that belong together (e.g. a whole street address). This is called "Smart Text Selection".

This is how you register a new TextClassifier:

TextClassificationManager manager = 
    (TextClassificationManager) getSystemService(Context.TEXT_CLASSIFICATION_SERVICE);
TextClassifier c = manager.getTextClassifier();
manager.setTextClassifier(new MyTextClassifier());

MyTextClassifier implements the TextClassifier interface and implements suggestSelection() and classifyText().

classifyText() returns a TextClassification object which has a title and an Intent which describes the contextual action to perform.

Here is my question (finally): If I register my TextClassifier with the TextClassificationManager it works for my own app only. Is there any way for defining a TextClassifier which can be used by other apps?

I would like to create an app which globally registers a new custom TextClassifier.

That way I could write an app that adds system-wide support for, let's say, song names (assuming I have a list of all song names I want to support). EditTexts would automatically select "Smells Like Teen Spirit" even though the user just long-clicked "Teen". And a contextual action "Search on Google Play Music" could pop up next to "Cut", "Copy" etc.

1 Answers

Unfortunately, in Android O, you won't be able to make other apps use your custom TextClassifier.

But if you'd like to encourage other apps to take advantage of your custom TextClassifier, you can create a library with your custom TextClassifier that can be built into other apps and set as the TextClassifier.

Related