Joshua Best
Joshua Best

Reputation: 246

Checking for internet in the background

I would like to be able to run a background process that is always open to check for when the user gets internet.

This is so that when the user comes back online, data I have saved to their phone locally will then automatically push its self to my online database.

I don't know if this is possible without having the app open.

Realm isn't an option due to GDPR

Thank you.

Upvotes: 1

Views: 75

Answers (1)

Susmit Agrawal
Susmit Agrawal

Reputation: 3764

Use this BroadcastReceiver structure:

public class InternetReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        //Do Something
    }
}

In the <application> tag, add:

<receiver android:name=".InternetReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
</receiver>

Upvotes: 4

Related Questions