1

In Java we can get the highlighted text from native window by using JNA or JNI. For example we could use Monitor text that is highlighted

Is there any way to do the same thing by using Android SDK??

Community
  • 1
  • 1
Hamreen Ahmad
  • 522
  • 5
  • 21

2 Answers2

3

You are welcome to use the accessibility APIs to write an accessibility service and watch for text selection events. This will require the user to agree to allow your app to spy on all user input, which will tend to make your app less popular.

Otherwise, this is not possible, for obvious privacy and security reasons.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • means there is not any legal way to get the selected text ? – Hamreen Ahmad Sep 25 '15 at 17:53
  • @HamreenAhmad: The accessibility APIs are certainly "legal". They just are very scary, and so users may elect to not use your app as a result. – CommonsWare Sep 25 '15 at 18:00
  • Regarding the accesibility framework, it's true, but if I'm not mistaken it should only listen to apps that are programmed as "accessible", popular Apps may be so. Checkout https://developer.android.com/training/accessibility/service.html – Jorch914 Sep 25 '15 at 19:51
  • @Jorch914: "but if I'm not mistaken it should only listen to apps that are programmed as "accessible", popular Apps may be so" -- all stock Android widgets are basically accessible by default. How easily they can be used by the visually or motor impaired will depend a bit on how well the app is written, and custom widgets require specific accessibility support. – CommonsWare Sep 25 '15 at 20:14
  • @CommonsWare how to get selected item text? Being Stuck for last two days – UMESH0492 Jun 13 '16 at 18:42
  • 1
    @UMESH0492: I assume that it is possible: https://developer.android.com/reference/android/view/accessibility/AccessibilityEvent.html#TYPE_VIEW_TEXT_SELECTION_CHANGED – CommonsWare Jun 13 '16 at 18:55
0

Using purely the SDK without exploiting a security vulnerability is not possible. There is a simple explanation of why this is not possible.

The first reason is the way Android apps are executed in the OS in a sandboxed way using linux's user groups and permission system. Every running process and Application on Android has it's own user and group and permissions to access those resources only. So in a way they cannot communicate with other apps(or capture what a user is highlithing at the moment. The only way for an app to communicate with other is using the binder IPC, which has to pass through the activity manager first. As far as my knowledge goes, there is not a defined way to do this. Notice this is a layer of protections inherited from linux below Android's usual permission system.

Adding to this, starting from Android 5(lollipop) add to this layer the now enforced selinux policies, which do not allow the application domain to access other domains that handle graphics, the mediaserver, and some others, I will not enter more in this topic due to it's complexity and relevance to the question, just know that these are some very secure mechanisms that prevent actions that might imply a security breach.

Is it possible? Yes, however it involes exploiting a vulnerability, but this is another topic and for that I should recommend to search papers on the web that talk about vulnerabilities in android.

Jorch914
  • 3,465
  • 2
  • 16
  • 21
  • means there is not any legal way to get the selected text ? – Hamreen Ahmad Sep 25 '15 at 17:53
  • No, it is more of a security breach on user builds. It is possible on rooted egineering builds since you may supress all this security layers. But it would be a more vulnerable and easy to exploit device. – Jorch914 Sep 25 '15 at 17:56