Support Android for incoming network connections?

I am writing an HTTP server for Android devices implemented via NanoHTTPD.

My goal is for the device to allow incoming connections even when the screen is off.

I started out small, with a constant notification, assuming that my app would stay in memory and run in the background. After locking the device, I could continue to navigate the web pages it serves until I leave it alone for about a minute. Once I do this, it stops responding completely.

I sharpened my attempt by enabling a partial CPU wakelock which made no difference. Then I added a full WifiLock to enable the radio and finally, desperately, MulticastLock (I thought maybe this would support radio listening for connections). However, after missing any connections for about a minute, the device stops responding even to all these locks.

Is there something specific that I can do to prevent the device from listening to incoming connections? It seems that tapping the device with intermittent prompts keeps it from waking up ... Can I mimic this behavior in some way? I can't think of a way.

Thank!

EDIT: For the purposes of this question, battery drain can be ignored.

EDIT: NanoHTTPD also runs as a service.

+3


source to share


1 answer


You need the following:

  • Foreground service
  • Wifi blocking
  • Locking the processor


Incomplete snippets (e.g. no services / denied locks):

    // Keep the CPU awake.
    powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Httpd");
    wakeLock.acquire();

    // Keep the wifi awake.
    WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "Httpd");
    wifiLock.acquire();

      

+1


source







All Articles