Android uses Brocastreceiver to monitor network connection status

Source: Internet
Author: User

 Public classBroadcastserviceextendsservice{PrivateConnectivitymanager Connectivitymanager;//Network Connection Manager    PrivateNetworkinfo Networkinfo;//information about the current network//Click to view    PrivatePendingintent messagependingintent =NULL; //Notification Bar Messages    PrivateNotification messagenotification =NULL; PrivateNotificationmanager Messagenotificatiomanager =NULL; PrivateIntent notificationintent=NULL; @Override Publicibinder onbind (Intent Intent) {//TODO auto-generated Method Stub        return NULL; }    /*** Define a broadcastreceiver*/    PrivateBroadcastreceiver myreceiver=NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {//TODO auto-generated Method StubString action=intent.getaction (); if(Action.equals (connectivitymanager.connectivity_action)) {Connectivitymanager=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Wifimanager Wifimanager=(Wifimanager) Context.getsystemservice (Context.wifi_service); Wifimanager.setwifienabled (true); Wifimanager.startscan ();//Start ScanningList<scanresult> listresults=wifimanager.getscanresults (); //listresults.get (0). toString ();networkinfo=Connectivitymanager.getactivenetworkinfo (); if(networkinfo!=NULL&&networkinfo.isavailable ()) {LOG.D ("Networkinfo", "Current network connection:" +networkinfo.gettypename ()); }                Else{LOG.D ("Networkinfo", "Currently no network Connection");    }                            }        }    }; @Override Public voidonCreate () {//TODO auto-generated Method Stub        Super. OnCreate (); Intentfilter Filter=NewIntentfilter ();//Filter IntentFilter.addaction (connectivitymanager.connectivity_action);//Add ActionRegisterreceiver (myreceiver, filter);//Register receiverLOG.I ("tag", "OnCreate"); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {        //TODO auto-generated Method StubLOG.I ("tag", "Onstartcommand"); Setnotification ("New Notice"); /*int intgettastcounter=30;        Activitymanager Mactivitymanager = (activitymanager) getsystemservice (Activity_service);        arraylist<string> arylisttask = new arraylist<string> ();        list<activitymanager.runningtaskinfo> mrunningtasks = Mactivitymanager.getrunningtasks (IntGetTastCounter);         int i = 0;                     Get work name and ID for (activitymanager.runningtaskinfo amtask:mrunningtasks) in cyclic and baseactivity way { Baseactivity.getclassname Remove Run Job name Arylisttask.add ("" + (i++) + ":" + amtask.b                    Aseactivity.getclassname () + "(id=" + amtask.id + ")");        LOG.D ("Task", Arylisttask.get (i-1)); }*/        return Super. Onstartcommand (Intent, flags, Startid); }    /*** SET notification message *@paramMessage Type*/    Private voidsetnotification (String message) {notificationintent=NewIntent ( This, Secondactivity.class); Messagependingintent= Pendingintent.getactivity ( This, 0,notificationintent,0); Messagenotificatiomanager=(Notificationmanager) Getsystemservice (Notification_service); Messagenotification=NewNotification (r.drawable.ic_launcher, message, System.currenttimemillis ()); //messagenotification.contentintent=messagependingintent;Messagenotification.setlatesteventinfo ( This, message, "DFDF", messagependingintent); //Show NotificationsMessagenotificatiomanager.notify ("DF", R.drawable.ic_launcher, messagenotification); } @Override Public voidOnDestroy () {//TODO auto-generated Method StubSystem.out.println ("Close to the hotel."); Unregisterreceiver (myreceiver);//to remove a registered receiverSystem.exit (0); Super. OnDestroy (); }    }

Android uses Brocastreceiver to monitor network connection status

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.