android - Broadcast receiver: check the wifi presence -
i have done code exploiting broadcast receiver in order call method on receive when status of connection change , show toast indicating change. in sense have separated class in implemented broadcast receiver in way:
public class receiver extends broadcastreceiver { private static final string debug_tag = "networkstatusexample"; @override public void onreceive(context context, intent intent) { // shows toast when connectivity state change toast.maketext(context, "stato wifi cambiato", toast.length_long).show(); log.d("prova", "action: " + intent.getaction()) } } it works recalling correctly method onreceive() when there network state change , showing toast.
in main file have put code once button pressed verifies if wifi connected or not in case showing toast message "wifi connected: true/false" depending on presence or not of connectivity. code works in case is:
public class mainactivity extends activity { private static final string debug_tag = "networkstatusexample"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); connectivitymanager connmgr = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo networkinfo = connmgr.getnetworkinfo(connectivitymanager.type_wifi); final boolean iswificonn = networkinfo.isconnected(); string wifistatetext = "no state"; log.d(debug_tag, "wifi connected: " + iswificonn); button prova = (button) findviewbyid(r.id.button1); prova.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { context context = getapplicationcontext(); charsequence text = "wifi connected: " + iswificonn; int duration = toast.length_short; toast toast = toast.maketext(context, text, duration); toast.show(); } }); } now i'm trying embedding last part of code in method onreceive() defined above in order let's in automatic once there network connectivity change ando resulting code of broadcast receiver one:
public class receiver extends broadcastreceiver { private static final string debug_tag = "networkstatusexample"; // verifica se il wifi รจ connesso @override public void onreceive(context context, intent intent) { // shows toast when connectivity state change toast.maketext(context, "stato wifi cambiato", toast.length_long).show(); log.d("prova", "action: " + intent.getaction()); connectivitymanager connmgr = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo networkinfo = connmgr.getnetworkinfo(connectivitymanager.type_wifi); final boolean iswificonn = networkinfo.isconnected(); string wifistatetext = "no state"; log.d(debug_tag, "wifi connected: " + iswificonn); // toast shows if wifi connected charsequence text = "wifi connected: " + iswificonn; int duration = toast.length_short; toast toast = toast.maketext(context, text, duration); toast.show(); } } in case error:
the method getsystemservice(string) undefined type receiver on line:
getsystemservice(context.connectivity_service); what's wrong? have fix it?
you can try :
context.getsystemservice(context.connectivity_service);
Comments
Post a Comment