So I'm trying to get what the user is typing on another app and if it matches my pre defined string, I need to fill it with something inside that text field without them having to open my app.
I tried using AutoFillService, but it's not working for some reasons,
<service
android:name=".MyServiceClass"
android:label="MyServiceClass"
android:permission="android.permission.BIND_AUTOFILL_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.service.autofill.AutofillService" />
</intent-filter>
<meta-data
android:name="android.autofill"
android:resource="@xml/service_configuration" />
</service>
In Java Service Class,
@RequiresApi(api = Build.VERSION_CODES.O)
public class MyServiceClass extends AutofillService {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onConnected() {
super.onConnected();
}
@Override
public void onFillRequest(@NonNull FillRequest fillRequest, @NonNull CancellationSignal cancellationSignal, @NonNull FillCallback fillCallback) {
List<FillContext> fillContexts = fillRequest.getFillContexts();
Dataset dataset = null;
for (FillContext context : fillContexts) {
AssistStructure structure = context.getStructure();
Bundle extras = structure.getWindowNodeAt(0).getRootViewNode().getExtras();
System.out.println(extras);
}
}
@Override
public void onSaveRequest(@NonNull SaveRequest saveRequest, @NonNull SaveCallback saveCallback) {
}
}
So when I enter any texts on another app, I'm not getting any signals on my service class. As per my knowledge it only works when the third party app uses the requestAutofill(); function or something, but tried using it on an app that requires email and password, but i still didn't get any callbacks.
Also is there any ways to get the texts from a textfield of any app, doesn't matter if they use requestAutoFill or not?
For example, getting text from what the user is typing in whatsapp edittext field..
Is that possible in Android?
In addition to what you declared in Manifest, user have to enable you service as preferred autofill method in device settings. Please refer to official documentation Build Autofill Services for more details if you haven't seen it yet.
But generally speaking, Autofill Service can't and shouldn't have access to all data entered by user in third party apps for security reasons. As you already mentioned, the service will receive a callback if a view in third party app explicitly declared as
android:importantForAutofill="yes".Also, you may be interested in this article - Integrate autofill with keyboards