Android network status judgment (WiFi, 3G, 2G)

Source: Internet
Author: User
Tags stub

First you need to add permissions

The code is as follows Copy Code

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

Check the network status code as follows

The code is as follows Copy Code

public boolean checknetworkstate ()
{
Boolean flag = false;
Connectivitymanager manager = (Connectivitymanager) getsystemservice (
Context.connectivity_service);
if (manager.getactivenetworkinfo ()!= null)
{
Flag = Manager.getactivenetworkinfo (). isavailable ();
}
if (!flag)
{
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.seticon (Android. R.drawable.ic_dialog_alert);
Builder.settitle (r.string.netstate);
Builder.setmessage (r.string.setnetwork);
Builder.setpositivebutton (R.string.ok, New Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Intent mintent = new Intent ("/");
ComponentName comp = new ComponentName ("Com.android.settings",
"Com.android.settings.WirelessSettings");
Mintent.setcomponent (comp);
Mintent.setaction ("Android.intent.action.VIEW");
StartActivity (mintent);
}
});
Builder.setnegativebutton (R.string.cancel, New Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
Dialog.cancel ();
}
});
Builder.create ();
Builder.show ();
}

return flag;

}

The results are as follows: Users can click OK to set up the network, such as starting WiFi

-------------------------------------------------------------------------------------------------
If you want to do something with a network connection, you need to determine whether the current network state is true, do something about it, or set up a network. It could be another way, actually.

The code is as follows Copy Code

Setnetwork
public void Setnetwork ()
{
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.seticon (Android. R.drawable.ic_dialog_alert);
Builder.settitle (r.string.netstate);
Builder.setmessage (r.string.setnetwork);
Builder.setpositivebutton (R.string.ok, New Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Intent mintent = new Intent ("/");
ComponentName comp = new ComponentName ("Com.android.settings",
"Com.android.settings.WirelessSettings");
Mintent.setcomponent (comp);
Mintent.setaction ("Android.intent.action.VIEW");
StartActivity (mintent);
}
});
Builder.setnegativebutton (R.string.cancel, New Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
Dialog.cancel ();
}
});
Builder.create ();
Builder.show ();
}

Network
public boolean isnetworkavailable ()
{
Context context = Getapplicationcontext ();
Connectivitymanager connect = (Connectivitymanager) context.getsystemservice (
Context.connectivity_service);
if (connect==null)
{
return false;
}else//get All network Info
{
Networkinfo[] info = Connect.getallnetworkinfo ();
if (info!=null)
{
for (int i=0;i<info.length;i++)
{
if (Info[i].getstate () ==networkinfo.state.connected)
{
return true;
}
}
}
}
return false;
}


Then, by judging if the current state is available, execute the relevant code, and then set up the network

The code is as follows Copy Code

if (isnetworkavailable ())
{
Related code
}else
{
Setnetwork ();
}]

This article has introduced the network state of the judgment, but after testing found that the method can only determine whether WiFi connectivity, and now the smart machine can be WiFi, 3G, or even 2G. So if you don't use the above method, you need to make changes so that it's not connected to the network (WiFi, 3G, 2G). The code is as follows

The code is as follows Copy Code
Check Network
public void Checknetworkstate ()
{
Boolean flag = false;
Connectivitymanager manager = (Connectivitymanager) getsystemservice (
Context.connectivity_service);
state mobile = Manager.getnetworkinfo (Connectivitymanager.type_mobile). GetState ();
state WiFi = Manager.getnetworkinfo (Connectivitymanager.type_wifi). GetState ();
If the network status of 3G, WiFi, 2G and so on is connected, exit, otherwise the display prompts the information to enter the network Setup interface
if (mobile = state.connected| | mobile==state.connecting)
Return
if (WiFi = state.connected| | wifi==state.connecting)
Return
Showtips ();
}

private void Showtips ()
{
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.seticon (Android. R.drawable.ic_dialog_alert);
Builder.settitle (r.string.netstate);
Builder.setmessage (r.string.setnetwork);
Builder.setpositivebutton (R.string.ok, New Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
If there is no network connection, enter the network Setup interface
StartActivity (New Intent (settings.action_wireless_settings));
}
});
Builder.setnegativebutton (R.string.cancel, New Dialoginterface.onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
Dialog.cancel ();
}
});
Builder.create ();
Builder.show ();
}

The way is simpler and more useful, of course, the first article also has the merit, may add the 3G state check can
Note: Don't forget to add the permission

The code is as follows Copy Code

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

Related Article

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.