0

I have to show a notification when the device is connected to internet.
Do i need to run a service all the time for this?

joyalshark
  • 51
  • 9
  • You can run the service all the time. or you can just do intermediate check on the internet. – Smit Feb 21 '17 at 17:34

3 Answers3

1

ConnectivityReceiver.java

public class ConnectivityReceiver
    extends BroadcastReceiver {

public static ConnectivityReceiverListener connectivityReceiverListener;

public ConnectivityReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent arg1) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();

    if (connectivityReceiverListener != null) {
        connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
    }
}

public static boolean isConnected() {
    ConnectivityManager
            cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();
}


public interface ConnectivityReceiverListener {
    void onNetworkConnectionChanged(boolean isConnected);
}}

MyApplication.java

public class MyApplication extends Application {

private static MyApplication mInstance;

@Override
public void onCreate() {
    super.onCreate();

    mInstance = this;
}

public static synchronized MyApplication getInstance() {
    return mInstance;
}

public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
    ConnectivityReceiver.connectivityReceiverListener = listener;
}}

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    ...
<receiver
        android:name=".ConnectivityReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
...
</application>

Activity.class

public class MainActivity extends AppCompatActivity
    implements ConnectivityReceiver.ConnectivityReceiverListener {
...
@Override
protected void onResume() {
    super.onResume();

    // register connection status listener
    MyApplication.getInstance().setConnectivityListener(this);
}
...
@Override
public void onNetworkConnectionChanged(boolean isConnected) {
if(isConnected){
Toast.makeText(context, "Internet Connected", Toast.LENGTH_SHORT).show();
}else{Toast.makeText(context, "Internet not Connected", Toast.LENGTH_SHORT).show();}   
}}
Narendra Sorathiya
  • 3,770
  • 2
  • 34
  • 37
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – NathanOliver Feb 21 '17 at 18:30
  • accept & upvote answer if you really satisfied with this – Narendra Sorathiya Feb 22 '17 at 19:03
0

You need a broadcast receiver to receive network state changes then pass this data to service which is not a bound service and create a notification or you can create your notification in your broadcast receiver.

anzaidemirzoi
  • 386
  • 4
  • 13
0

You don't have to run a service to check for connection changes.

You can register a broadcast receiver with the android.net.conn.CONNECTIVITY_CHANGE intent.

Note that

Apps targeting Android 7.0 (API level 24) and higher do not receive this broadcast if they declare the broadcast receiver in their manifest. Apps will still receive broadcasts if they register their BroadcastReceiver with Context.registerReceiver() and that context is still valid.

Link for more information: https://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION

Similar question: https://stackoverflow.com/a/2295044/2174489

Community
  • 1
  • 1
Eduardo Herzer
  • 2,033
  • 21
  • 24
  • Only use such broadcast receiver when in app and the user may perform an action requiring an internet connection (posting a message, uploading a file etc.). Otherwise it's a useless information and constant listening for changes wastes battery. – Eugen Pechanec Feb 21 '17 at 18:09