简体   繁体   中英

Android: Activity establish network connection

I have an Activity which looks up data from the web in its onCreate method. The Activity is activated by the user hitting a notification. So it is a common problem that the user will quickly turn on their phone, unlock it, slide open notifications, tap the notification, and the Activity will activate before the phone is done connecting to internet.

I do have a friendly AlertDialog that pops up informing the user that the data couldn't be received and to try again when the network is connected; but is there a way for the Activity to actively tell the phone to connect and detect that a connection is being made and then wait for the connection to establish, and then load its data successfully ?

Usually, you would do something like this:

@Override
public void onResume(){
    super.onResume();
    // first, check connectivity
    if ( isOnline ){
        // do things if it there's network connection
    }else{
        // as it seems there's 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 ask the user to go and configure a network connection. Then, if the user does want to configure it, you will call the Settings.ACTION_WIRELESS_SETTINGS intent.

Also, notice the isOnline variable, which is a boolean that tells whether there's a network connection or not. In order to set that variable you can use an external simple class like this:

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();
    }
}

Also, you will have to add this permission to your AndroidManifest.xml file:

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

From this last statement by Cristian 'return info.isConnectedOrConnecting();' It seems you can do whatever you want to do with the activity based on the boolean value returned by the method. Since what you after is a positive return value you can use it's variable to callback your activity's next action

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM