|
| 1 | +# Android |
| 2 | + |
| 3 | +<!-- MACRO{toc} --> |
| 4 | + |
| 5 | +## How to keep a Socket.IO client running in the background? |
| 6 | + |
| 7 | +Long story short, you probably shouldn't. The Socket.IO client is not meant to be used in a [background service](https://developer.android.com/guide/components/services?hl=en), as it will keep an open TCP connection to the server and quickly drain the battery of your users. |
| 8 | + |
| 9 | +It is totally usable in the foreground though. |
| 10 | + |
| 11 | +See also: https://developer.android.com/training/connectivity |
| 12 | + |
| 13 | +## How to reach an HTTP server? |
| 14 | + |
| 15 | +Starting with Android 9 (API level 28) you need to explicitly allow cleartext traffic to be able to reach an HTTP server (e.g. a local server at `http://192.168.0.10`): |
| 16 | + |
| 17 | +- either for all domains: |
| 18 | + |
| 19 | +`app/src/main/AndroidManifest.xml` |
| 20 | + |
| 21 | +```xml |
| 22 | +<?xml version="1.0" encoding="utf-8"?> |
| 23 | +<manifest> |
| 24 | + <uses-permission android:name="android.permission.INTERNET" /> |
| 25 | + |
| 26 | + <application android:usesCleartextTraffic="true"> |
| 27 | + ... |
| 28 | + </application> |
| 29 | +</manifest> |
| 30 | +``` |
| 31 | + |
| 32 | +- or for a restricted list of domains: |
| 33 | + |
| 34 | +`app/src/main/AndroidManifest.xml` |
| 35 | + |
| 36 | +```xml |
| 37 | +<?xml version="1.0" encoding="utf-8"?> |
| 38 | +<manifest> |
| 39 | + <uses-permission android:name="android.permission.INTERNET" /> |
| 40 | + |
| 41 | + <application android:networkSecurityConfig="@xml/network_security_config"> |
| 42 | + ... |
| 43 | + </application> |
| 44 | +</manifest> |
| 45 | +``` |
| 46 | + |
| 47 | +`app/src/main/res/xml/network_security_config.xml` |
| 48 | + |
| 49 | +```xml |
| 50 | +<?xml version="1.0" encoding="utf-8"?> |
| 51 | +<network-security-config> |
| 52 | + <domain-config cleartextTrafficPermitted="true"> |
| 53 | + <domain includeSubdomains="true">localhost</domain> |
| 54 | + <domain includeSubdomains="true">192.168.0.10</domain> |
| 55 | + </domain-config> |
| 56 | +</network-security-config> |
| 57 | +``` |
| 58 | + |
| 59 | +Reference: https://developer.android.com/training/articles/security-config |
0 commit comments