How we can access WEbsocket and Http apis in Android system services
Asked Answered
W

1

0

I am working on custom ROM project and there is requirement to create service which can continues connected with our cloud server via Websocket or socket or MQTT like Google service is doing. For this use I have created AOSP system service.

I have successfully created system service inside the custom ROM and it's working fine but on this class I am unable to access websocket or HTTPAPIs. Whenever I am going to access those APIs the the aosp build was going to failed and throwing exception as below.

error: package java.net.http does not exist
import java.net.http.HttpClient;

error: package java.net.http does not exist
import java.net.http.WebSocket;

How can I access websocket and http APIs in android system service?

I am using Android-11 source code.

Woodbridge answered 6/6, 2022 at 8:49 Comment(2)
java.net.http.HttpClient was added in Java 11, but Android only supports Java 8 code. The full list of classes on Android is documented here: developer.android.com/reference/classesCavalla
I have posted the same thread on Reddit and there is small conversation for the same. May be this can help!! Link: reddit.com/r/androiddev/comments/v6245n/…Woodbridge
W
1

Why WebSocket and Http APIs are not accessible into the Android system service

From the response I have received by asking on Reddit it looks like making cloud communication on a system level on system services is not a good practice and will be break the security of android OS.

Apart from them those packages are not available on system services due to that reason the exception occurs during building of aosp custom ROM. Those HTTP and websocket packages are not available due to security reason.

Alternate Approach

We can make service inside the system application and enable flag of android:persistent="true" which will persistent application and the service will long run. On boot of the phone the Application class onCreate() method called. Here the service is not automatically start so on callback of onCreate() method inside Application class we have to start service which will be continues long running.

So we can choose this option to make cloud communication for this types of use case.

Woodbridge answered 9/6, 2022 at 4:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.