0

I just want to ask how to make an app need Internet Connection to launch and if user smartphone don't have connection so the app will show some attention like Clash of Clans game or another online game

enter image description here

If connection was lost or turn off, the app will show some attention like that

Anyone can explain or give some reference to how to make like that?

Ricci
  • 217
  • 5
  • 21
  • 1
    how to check internet connection -- http://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android -- http://stackoverflow.com/questions/31283443/check-network-and-internet-connection-android – Tasos Jan 22 '16 at 09:50
  • Is this your first Android app? Show us what you have tried so far. – Marko Popovic Jan 22 '16 at 09:50
  • If your app does indeed _need_ an internet connection to play, then, without one, the normal game loop is going to generate error conditions when it cannot talk to the server(s) it needs to. Calling an additional check won't guarantee that the connection won't be lost moments later. – TripeHound Jan 22 '16 at 10:06
  • [check internet](http://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android?lq=1) – nobalG Jan 22 '16 at 10:16

1 Answers1

1

Using BroadcastReceiver you can archive this.

public class InternetChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        final ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        final android.net.NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable() || mobile.isAvailable()) {
            // Open your alert dialog here.


        }
    }
}

In Manifest file register your BroadcastReceiver.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.checkinternet"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".InternetChangeReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />                
            </intent-filter>
        </receiver>
    </application>

</manifest>