virtual catty
virtual catty

Reputation: 1

Android studio Accessibility service network request not working when outside the MainActivity application context

I have registered an accessibility service in my Android application and attempted to add a floating button to collect some data and send it to the backend for processing while outside of my app environment. However, I noticed that when I leave my application context, all the requests sent end up TIMEOUT, and the backend does not seem to receive any of the requests.

Here is my code:

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadCurrentPage();
            }
        });

\\...
 public void uploadCurrentPage() {
        String info = "some info";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "https://" + Utility.IPAddress + ":" + Utility.Port + "/uploadPage";
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .connectTimeout(600, TimeUnit.SECONDS)
                        .readTimeout(200, TimeUnit.SECONDS)
                        .writeTimeout(200, TimeUnit.SECONDS)
                        .build();
                FormBody.Builder formBody = new FormBody.Builder();
                formBody.add("page", info);
                Request request = new Request.Builder()
                        .url(url)
                        .post(formBody.build())
                        .build();
                Call call = client.newCall(request);
                try {
                    call.execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

My AndoridManifest.xml is as follow:

    ...
    <uses-permission android:name="android.permission.INTERNET" />
    ...

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true"

            android:label="MyService"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">

            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>

            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/my_service_config" />
        </service>


<!--my_service_config.xml-->
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackAudible|feedbackSpoken|feedbackHaptic"
    android:accessibilityFlags="flagDefault|flagIncludeNotImportantViews|flagRequestEnhancedWebAccessibility|flagReportViewIds|flagRequestFilterKeyEvents|flagRetrieveInteractiveWindows"
    android:canRetrieveWindowContent="true"
    android:canRequestFilterKeyEvents="true"
    android:canRequestEnhancedWebAccessibility="true"
    android:notificationTimeout="0"
    android:canPerformGestures="true"
    android:description="@string/description"/>

The most thing that confused me is that this just worked for me several months ago with the same code. My API level is 31. Any suggestions may help me a lot , thanks!

I've tried to change 'call.execute()' into 'call.enqueue()', which made no sense for me.

Upvotes: 0

Views: 46

Answers (0)

Related Questions