Android: Network anytime need to switch between 3G and WiFi, network program needs attention

Source: Internet
Author: User

Normally, when 3G and WiFi are on, Android uses Wi-Fi by default, but it is not possible to have WiFi everywhere in the real world, so the phone will often automatically switch networks.

Sometimes, when the phone starts using WiFi, it will automatically switch from WiFi to 3G network 10-30 minutes after it enters standby.

If you write a network program, the effect of automatic network switching on the program is very obvious, the IP address will definitely change.

The feeling of the Android environment with our daily Windows environment is still very different, writing Android programs, obviously need to pay attention to the details there are many.

--------------------------------------------------------------------------------------------------------------- ---------------------------------

Some of the relevant code is attached below:
Listen network connection Status: All disconnected, currently using the WiFi network, is currently using the mobile network, which is mainly used for data communication line judgment

Updatereceiver receiver = new Updatereceiver ();

Filter.addaction (connectivitymanager.connectivity_action);

Registerreceiver (receiver, filter); Sign up and start answering the radio

Receive a broadcast message from a service
Class Updatereceiver extends Broadcastreceiver
{
private int previousnettype =-1;

@Override
public void OnReceive (context context, Intent Intent)
{
System.out.println ("Received broadcast message:" + intent + "------" + Intent.getextras ());

String action = Intent.getaction ();

if (Action.equals (connectivitymanager.connectivity_action))
{
Listen: Network status has changed, broken network or mobile network on-line or WiFi network on-line or mobile network and WiFi switching
Detailed network change parameter information can be referred to via Intent.getextras ()

Connectivitymanager Connectivitymanager = (connectivitymanager) getsystemservice (Context.connectivity_service);
Networkinfo mobilenetinfo = Connectivitymanager.getnetworkinfo (connectivitymanager.type_mobile);
Networkinfo wifinetinfo = Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi);

Networkinfo Networkinfo = (networkinfo) Intent.getparcelableextra (connectivitymanager.extra_network_info); The current
Networkinfo Othernetworkinfo = (networkinfo) Intent.getparcelableextra (Connectivitymanager.extra_other_network_ INFO); Another network status

Boolean hasnoconnected = Intent.getbooleanextra (connectivitymanager.extra_no_connectivity, false); Check if the network is all disconnected
Boolean isfailover = Intent.getbooleanextra (Connectivitymanager.extra_is_failover, false); is not WiFi and 3g in switch | Fail over
String reason = Intent.getstringextra (Connectivitymanager.extra_reason); Reason, change for what

if (hasnoconnected)
{
Previousnettype =-1;

Isnetonline = false;

if (currentfriend! = null)
{
Addsystemtiptotalk ("The network is disconnected, you are offline.", Currentfriend.getnumber ());
}

     //If the phone was previously over wifi and turned off the 3G data network, it may be a few minutes after the lock screen, Android will automatically turn off the WiFi connection and need a sound hint
     if (activityisstoped | | mkeyguardmanager.inkeyguardrestrictedinputmode ())
      {
      shownotify (a.id_notify_going, GetResources (). GetString (R.string.app_name), "The network is disconnected, you are offline.", "The network is disconnected, you are offline.", R.drawable.ic_stat_logo,
         notification.flag_no_clear);
     }
     else
     {
       showsystemtip ("The network is disconnected, you are offline.");
     }
    }
    else
    {
     isnetonline = true;

if (Isfailover | | othernetworkinfo = = NULL)
{
if (networkinfo.gettype () = = Connectivitymanager.type_wifi)
{
if (previousnettype! = Connectivitymanager.type_wifi && networkinfo.isconnected ())
{
Previousnettype = Connectivitymanager.type_wifi;

if (currentfriend! = null)
{
Addsystemtiptotalk ("WiFi network is enabled.", Currentfriend.getnumber ());
}

Showsystemtip ("WiFi network is enabled");

Because just switch network, can not send data immediately, wait a few seconds after the hair, more reliable
New Thread (New Runnable () {
public void Run ()
{
Try
{
Thread.Sleep (2000);
}
catch (Interruptedexception e)
{
}
Sendbroadcast (Xintiaoservice.intent_xintiao); Notifies the server that I switched the network
}
}). Start ();

}
}
Else
{
if (Previousnettype = = Connectivitymanager.type_wifi | | previousnettype = =-1)
{
if (networkinfo.isconnected ())
{
Previousnettype = Connectivitymanager.type_mobile;

if (currentfriend! = null)
{
Addsystemtiptotalk (Networkinfo.getsubtypename () + "network enabled.", Currentfriend.getnumber ());
}

Showsystemtip (Networkinfo.getsubtypename () + "network Enabled");

Because just switch network, can not send data immediately, wait a few seconds after the hair, more reliable
New Thread (New Runnable () {
public void Run ()
{
Try
{
Thread.Sleep (2000);
}
catch (Interruptedexception e)
{
}
Sendbroadcast (Xintiaoservice.intent_xintiao); Notifies the server that I switched the network
}
}). Start ();
}
}
}
}

2011-11-27

Android: Network anytime need to switch between 3G and WiFi, network program needs attention

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.