Android gets current network connection information and network link status GPRS and WiFi network

package com.tyy.testconnectivitymanager;

import java.util.Timer;
import java.util.TimerTask;

import android.media.audiofx.BassBoost.Settings;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android .os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Get the current network connection information and network link status GPRS and WiFi network
 *
 * If you need to get the current network connection status, you need to use a class, ConnectivityManager to get the connected object
 *
 * To get the current network connection status, you need to specify permissions
 * */
public class MainActivity extends Activity {
    private TextView gprs;
    private TextView wifi;
    private ConnectivityManager manager;// Network connection management object

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout .activity_main);
        gprs = (TextView) findViewById(R.id.gprs);
        wifi = (TextView) findViewById(R.id.wifi);
        // Initialization of the
        manager manager = (ConnectivityManager) this
                .getSystemService(Context.CONNECTIVITY_SERVICE );
        // Obtain the network connection information of this type according to the given network type, and further obtain the network status in the current information
        State gprsstate = manager.getNetworkInfo(
                ConnectivityManager.TYPE_MOBILE).getState();

        State wifiState = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                .getState();

        wifi.setText("The current WiFi state is" + wifiState.toString()) ;
        gprs.setText("The current GPRS state is" + gprsstate.toString());

        // There is currently no network connection
        if (!gprsstate.equals(State.CONNECTED)
                && !wifiState.equals(State.CONNECTED)) {
            Toast.makeText(MainActivity.this, "The current network has no connection and jump to the network connection page", 0).show();
            Timer timer = new Timer();
            //Delay the task execution
            timer.schedule(new MyTask() , 5000);
            
        }

    }

    //Performed task class
    private class MyTask extends TimerTask{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            // Jump to the network setting interface
            Intent intent = new Intent();
            intent.setAction( android.provider.Settings.ACTION_WIRELESS_SETTINGS);
            // The action of the wireless network connection
            startActivity(intent);
        }
        
    }
}
 There are only two textviews on the interface

Guess you like

Origin blog.csdn.net/qq_37790902/article/details/79583673