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 ();
}
}