WiFi activation and Connection

Source: Internet
Author: User

Public class wifialarmreceiver extends broadcastreceiver {
 
Public Static Context sendercontext;
 
Public wifialarmreceiver ()
{
Log. I ("alarmreceiver. alarmreceiver ()", calendar. getinstance (). gettime (). tolocalestring ());
Log. I ("alarmreceiver. alarmreceiver ()-> PID", String. valueof (Android. OS. process. mypid ()));
Log. I ("alarmreceiver. alarmreceiver ()-> TID", String. valueof (Android. OS. process. mytid ()));
}

Public void onreceive (context, intent ){
Sharedpreferences DATA = context. getsharedpreferences (flightandwifiactivity. flight_wifi_name, 0 );
Boolean iswifion = data. getboolean (flightandwifiactivity. is_wifi_on, flightandwifiactivity. default_is_on );
If (iswifion ){
Closewifi (sendercontext );
} Else {
Openwifi (sendercontext );
// Showtoast (sendercontext, "onreceive (), openwifi trunk ");
Log. I ("tag", "onreceive (), openwifi trunk ");
}

}

Private void openwifi (context ){

Boolean ISON = false;
Wifimanager mwifimanager = (wifimanager) Context. getsystemservice (context. wifi_service );
// Showtoast (sendercontext, "openwifi trunk." + mwifimanager. getwifistate ());
Try {
If (! Mwifimanager. iswifienabled () & mwifimanager. getwifistate ()! = Wifimanager. wifi_state_enabling ){
If (mwifimanager. setwifienabled (true )){
Switch (mwifimanager. getwifistate ()){
Case wifimanager. wifi_state_enabling:
Showtoast (context, context. getresources (). getstring (R. String. wifi_enabling ));
ISON = true;
Break;
Case wifimanager. wifi_state_enabled:
Showtoast (context, context. getresources (). getstring (R. String. wifi_open_success ));
ISON = true;
Break;
Default:
Showtoast (context, context. getresources (). getstring (R. String. wifi_open_fail) + ":" + context. getresources (). getstring (R. String. wifi_unknow ));
Break;
}
} Else {
Showtoast (context, context. getresources (). getstring (R. String. wifi_open_fail ));
}
} Else {
// Showtoast (sendercontext, "openwifi (), fffff ");
// Mchkwifi. setchecked (false );
Switch (mwifimanager. getwifistate ()){
Case wifimanager. wifi_state_enabling:
Showtoast (context, context. getresources (). getstring (R. String. wifi_open_fail) + ":" + context. getresources (). getstring (R. String. wifi_enabling ));
Break;
Case wifimanager. wifi_state_disabling:
Showtoast (context, context. getresources (). getstring (R. String. wifi_open_fail) + ":" + context. getresources (). getstring (R. String. wifi_disabling ));
Break;
Case wifimanager. wifi_state_enabled:
Showtoast (context, context. getresources (). getstring (R. String. wifi_open_fail) + ":" + context. getresources (). getstring (R. String. wifi_enabled ));
ISON = true;
Break;
Case wifimanager. wifi_state_disabled:
Showtoast (context, context. getresources (). getstring (R. String. wifi_open_fail) + ":" + context. getresources (). getstring (R. String. wifi_disabled ));
Break;
Case wifimanager. wifi_state_unknown:
Default:
Showtoast (context, context. getresources (). getstring (R. String. wifi_open_fail) + ":" + context. getresources (). getstring (R. String. wifi_unknow ));
Break;
}
}

} Catch (exception ex ){
Ex. printstacktrace ();
}

If (ISON ){
Editor edit = context. getsharedpreferences (flightandwifiactivity. flight_wifi_name, 0). Edit ();
Edit. putboolean (flightandwifiactivity. is_wifi_on, true );
Edit. Commit ();
} Else {

}
}

Private void closewifi (context ){
Boolean isoff = false;
Wifimanager mwifimanager = (wifimanager) Context. getsystemservice (context. wifi_service );
Try {
If (mwifimanager. iswifienabled () {// If WiFi is enabled, you can disable it.
If (mwifimanager. setwifienabled (false )){
Showtoast (context, context. getresources (). getstring (R. String. wifi_close_success ));
Isoff = true;
} Else {
Showtoast (context, context. getresources (). getstring (R. String. wifi_close_fail ));
}
} Else {
Switch (mwifimanager. getwifistate ()){
Case wifimanager. wifi_state_enabling:
Showtoast (context, context. getresources (). getstring (R. String. wifi_close_fail) + ":" + context. getresources (). getstring (R. String. wifi_disabling ));
Break;
Case wifimanager. wifi_state_disabling:
Showtoast (context, context. getresources (). getstring (R. String. wifi_close_fail) + ":" + context. getresources (). getstring (R. String. wifi_disabling ));
Break;
Case wifimanager. wifi_state_disabled:
Showtoast (context, context. getresources (). getstring (R. String. wifi_close_fail) + ":" + context. getresources (). getstring (R. String. wifi_disabled ));
// Isoff = true;
Break;
Case wifimanager. wifi_state_unknown:
Default:
Showtoast (context, context. getresources (). getstring (R. String. wifi_close_fail) + ":" + context. getresources (). getstring (R. String. wifi_unknow ));
Break;
}
}
} Catch (exception ex ){
Ex. printstacktrace ();
}

If (isoff ){
Sharedpreferences DATA = context. getsharedpreferences (flightandwifiactivity. flight_wifi_name, 0 );
Int timesleft = data. getint (flightandwifiactivity. wifi_times_left, flightandwifiactivity. default_edittext_value );
Timesleft --;
Editor edit = data. Edit ();
Edit. putboolean (flightandwifiactivity. is_wifi_on, false );
Edit. putint (flightandwifiactivity. wifi_times_left, timesleft );
Edit. Commit ();
Intent intent = new intent (flightandwifiactivity. action_update_wifi_timesleft );
Context. sendbroadcast (intent );
} Else {

}
}

Private void showtoast (context, string MSG ){
Toast. maketext (context, MSG, Toast. length_short). Show ();
}

}

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.