I am implementing a mobile application in android studio, that is tracking user's location. I collect the locations of the user in a file and when the location tracking is stopped I send the locations in a database one by one. I want to implement a "forwarder" in order to be able to keep this file with locations when there is no internet connection and when the mobile is connected to internet automatically sends the data to the database. Can anyone please give some clues how to implement this "forwarder"?
Asked
Active
Viewed 194 times
0
-
If you need to save data and then be able to push it to remote server you could just use `Room` to save locations and then use `Retrofit` or something related to send information to the server. So you just need to specify, when and how data will be added into the database and then just provide the logic how t upload it to the server using Http client. For that you could use background processing or `WorkManager`. – Blind Kai Aug 19 '19 at 10:27
2 Answers
0
i think its should be simple. you can have a thread , that check the connection status one a minute for example , and send the data where is intenet connection.

maor ts
- 36
- 3
0
You can try implementing a broadcast receiver for that.
Register a BroadcastReceiver in Manifest.file
<receiver android:name=".MyBroadcastReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
Set Manifest internet permissions
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET"/>
Implement receiving class.
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Do forwarding here } }

Pradeep
- 261
- 2
- 7
-
Thank you for your answer! If the application is stopped, not running in background, will the receiver still be able to detect internet connection and forward the locations?? – p20 Aug 19 '19 at 12:04
-
That based on android distribution also. Otherwise you can try it as service. Refer the discussion, https://stackoverflow.com/questions/16824341/keep-broadcast-receiver-running-after-application-is-closed – Pradeep Aug 19 '19 at 13:25
-
If any of the answers helped in solving your problem, pls update by voting. – Pradeep Aug 24 '19 at 09:13