2

I need to upload a record of data from local DB to the server which also includes a video file which may be of about 30 MB size. Above upload happens when the network gets available even when the app is killed. Now for detecting network change, in Android N and above CONNECTIVITY_CHANGE and CONNECTIVITY_ACTION are deprecated, so broadcast receiver is not useful. also, the CONNECTIVITY_ACTION is deprecated in API 28 (Pie).

Many answers suggest to use JobSchedular for such tasks but it is useful for smaller uploads.

refered this: Detect CONNECTIVITY CHANGE in Android 7 and above when app is killed/in background

WorkManager https://developer.android.com/topic/libraries/architecture/workmanager.html

So how can I detect the network change in Pie(API 28) when app is killed and perform the above mentioned upload task.

CCMob
  • 23
  • 7

1 Answers1

2

JobScheduler or WorkManager has a timeout of 10 minutes. If the upload takes longer than that, then you must use Foreground Service!

Considering the device's limited resources, starting from Android 8.0, Google has taken some strict measures in managing background task for the app. For any long running task, Apps must use Foreground Service so that users are always aware of it.

So after scheduling your task using either WorkManager or JobScheduler, you can start a Foreground Service in which you can do the process of uploading the video. (reference)

and in your Service you can use ConnectivityManager.registerNetworkCallback to listen for network connection. (reference)

Binary Baba
  • 1,953
  • 2
  • 16
  • 23
  • thanks for suggestions! but as I have mentioned for detecting network change the reference you have given will work upto Oreo but not in Pie as CONNECTIVITY_ACTION is deprecated in API 28. so I am looking for much reliable solution for this which works in Pie. BTW did you just rename your profile from Rick? – CCMob Apr 04 '19 at 14:13
  • The reference I gave talks about CONNECTIVITY_ACTION only for API level below 21. But you can definitely use `ConnectivityManager.registerNetworkCallback` or `registerDefaultNetworkCallback` even in API 28 ([reference](https://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION)) and yes I renamed my profile to Girish (my actual name) to give it a more personal touch :) – Binary Baba Apr 05 '19 at 02:01
  • thanks Girish! I tested it in a Pie device and the approach works to detect network change. But my concern is will it be still able to work if the WorkManager is doing upload work in foreground service and the app is killed, then will WorkManager be still running or will it resume once the app is started again as it was a Foreground service? – CCMob Apr 05 '19 at 07:57
  • Foreground Service will not resume automatically once the app is started again. You will have to maintain some download state and explicitly start the foreground service once the app is started in the next session. – Binary Baba Apr 06 '19 at 15:55