Android: activity establishes network connection

I have an Activity that is looking for data from the Internet in the onCreate method. The user activates the notification. Thus, a common problem is that the user quickly turns on his phone, unlocks it, open notifications, tap the notification and activity is activated before the phone is connected to the Internet.

I have a friendly AlertDialog that appears informing the user that the data cannot be received, and try again when the network is connected; but is there a way for Activity to actively inform the phone about the connection and find out that the connection is being made, and then wait for the connection to be established and then successfully download its data ?

+3
source share
2 answers

Usually you do something like this:

@Override
public void onResume(){
    super.onResume();
    // first, check connectivity
    if ( isOnline ){
        // do things if it there network connection
    }else{
        // as it seems there no Internet connection
        // ask the user to activate it
        new AlertDialog.Builder(YourActivity.this)
            .setTitle("Connection failed")
            .setMessage("This application requires network access. Please, enable " +
                    "mobile network or Wi-Fi.")
            .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // THIS IS WHAT YOU ARE DOING, Jul
                    YourActivity.this.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    YourActivity.this.finish();
                }
            })
            .show();
    }
}

The idea is to ask the user to go over and set up a network connection. Then, if the user wants to configure it, you name the intent Settings.ACTION_WIRELESS_SETTINGS.

Also pay attention to the variable isOnline, which is a boolean that reports whether there is a network connection or not. To set this variable, you can use an external simple class as follows:

public class CheckConnectivity {
    public static boolean isOnline(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if( cm == null )
            return false;
        NetworkInfo info = cm.getActiveNetworkInfo();
        if( info == null )
            return false;
        return info.isConnectedOrConnecting();
    }
}

In addition, you will need to add this permission to your AndroidManifest.xmlfile:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+4
source

 ' info.isConnectedOrConnecting();" , , , , . , .

+2

Source: https://habr.com/ru/post/1756345/


All Articles