Android detects if the network is connected

Source: Internet
Author: User

Permissions:

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

The code is as follows:

Package com.example.nettest;

Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo.State;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.ComponentName;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.view.Menu;
Import Android.widget.TextView;
Import Android.widget.Toast;


public class Mainactivity extends Activity {


Private Connectivitymanager Manager;
Private TextView TV;
StringBuffer sb = new StringBuffer (256);


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);


TV = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Checknetworkstate ();
}


/**
* Detects if the network is connected
*
* @return
*/
Private Boolean checknetworkstate () {
Boolean flag = false;
Get Network connection Information
Manager = (Connectivitymanager) getsystemservice (Context.connectivity_service);
To determine if the network is connected
if (manager.getactivenetworkinfo () = null) {
Flag = Manager.getactivenetworkinfo (). isavailable ();
}
if (!flag) {
Setnetwork ();
} else {
Isnetworkavailable ();
}
Tv.settext (Sb.tostring ());
return flag;
}


/**
* Setting method is called when the network is not connected
*/
private void Setnetwork () {
Toast.maketext (This, "Wi-Fi is closed!", Toast.length_short). Show ();
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.seticon (R.drawable.ic_launcher);
Builder.settitle ("Network alert Information");
Builder.setmessage ("Network is not available, if you continue, please set up the network first!") ");
Builder.setpositivebutton ("Settings", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Intent Intent = null;
/**
* Determine the version of the phone system! If the API is greater than 10 is 3.0+ because the settings of more than 3.0 are not the same as the settings below 3.0, the method is called differently
*/
if (Android.os.Build.VERSION.SDK_INT > 10) {
Intent = new Intent (
Android.provider.Settings.ACTION_SETTINGS);
} else {
Intent = new Intent ();
ComponentName component = new ComponentName (
"Com.android.settings",
"Com.android.settings.WirelessSettings");
Intent.setcomponent (component);
Intent.setaction ("Android.intent.action.VIEW");
}
StartActivity (Intent);
}
});


Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
}
});
Builder.create ();
Builder.show ();
}


/**
* The network has been connected, then to determine whether the WiFi connection or GPRS connection set some of their own logical calls
*/
private void isnetworkavailable () {


state GPRS = Manager.getnetworkinfo (connectivitymanager.type_mobile)
. GetState ();
state WiFi = manager.getnetworkinfo (Connectivitymanager.type_wifi)
. GetState ();
if (GPRS = = State.connected | | gprs = = state.connecting) {
Toast.maketext (This, "GPRS is open!", Toast.length_short). Show ();
Sb.append ("\ngprs is open!");
} else {
Sb.append ("\ngprs is closed!");
}


Judge the WiFi status to load ads, if it is GPRS mobile network does not load!
if (WiFi = = state.connected | | wifi = = state.connecting) {
Toast.maketext (This, "Wi-Fi is open!", Toast.length_short). Show ();
Loadadmob ();
Sb.append ("\nwifi is open!");
} else {
Sb.append ("\nwifi is closed!");
}


}


/**
* Load AdMob ads in WiFi status
*/
private void Loadadmob () {
Toast.maketext (Getapplicationcontext (), "Ad is loding:", 1). Show ();
Sb.append ("\nad is loding ...");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.