i there way flag if wifi connection got disconnected/ dropped off or if user changed wifi network ?
i need app : connect wifi xyz, if xyz disconnect (flag 1) or dropped off reconnect xyz. user change wifi btopen (flag 2) allow connect , stop service. if user connect xyz again start loop again.
what got far :
<!-- wifi receiver --> <receiver android:name=".receiverwifi" > <intent-filter> <action android:name="android.net.wifi.wifi_state_changed" /> <action android:name="android.net.conn.connectivity_change" /> </intent-filter> </receiver> <service android:name=".servicewifimonitor" /> <receiver android:name=".servicecontroller" > <intent-filter > <action android:name="android.intent.action.boot_completed" /> <category android:name="android.intent.action.quickboot_poweron" /> </intent-filter> </receiver>
broadcastreceiver:
myapplication = (myapplication) context.getapplicationcontext(); conmanager = (connectivitymanager) context.getsystemservice(context.connectivity_service); wifimanager = (wifimanager) context.getsystemservice(context.wifi_service); networkinfo = conmanager.getnetworkinfo(connectivitymanager.type_wifi); boolean isconnected = networkinfo != null && networkinfo.isconnected(); int reconnectedcount = myapplication.getreconnectedcount(); if (wifimanager.iswifienabled()) { if("android.net.conn.connectivity_change".equals(intent.getaction())) { //start , stop service if(myapplication.isreconnect()) startservicewifimonitor(); else stopservicewifimonitor(); if (isconnected) { //there wifi connection myapplication.setconnectedwifi(networkutil.getcurrentssid(context)); myapplication.setwifistatus("connected"); if (networkutil.isconnectedtoxyz(context)) { startservicewifimonitor(); if(pref.getisfirsttime()) { myapplication.setwifibychoise("xyz"); pref.setisfirsttime(false); } else { myapplication.setisreconnect(true); } } else { //connected different network if(myapplication.isreconnect() && networkutil.isxyzavailable(context)) { //reconnect xyz networkutil.connecttoxyz(context); myapplication.setreconnectedcount(reconnectedcount++); } else { resetvalues("aaaa"); } } }//end if else { if(networkutil.isxyzavailable(context) && myapplication.getwifibychoise().equals("xyz")) { networkutil.connecttoxyz(context); myapplication.setreconnectedcount(reconnectedcount++); } else { resetvalues(""); } } }//end connectivity_change
service monitor:
@override public int onstartcommand(intent intent, int flags, int startid) { log.i(tag, "onstartcommand > received start id " + startid + ": " + intent); objhandler.postdelayed(mtasks, 1000); return start_sticky; }//end onstartcommand private runnable mtasks = new runnable() { public void run() { if(myapplication.getwifibychoise().equals("xyz") && networkutil.isxyzavailable(context)) { try { //get numbers of reconnection int count = myapplication.getreconnectedcount(); if(!networkutil.iswificonnected(context)) { networkutil.connecttoxyz(context); myapplication.setisreconnect(true); myapplication.setreconnectedcount(count++); } if(!networkutil.isconnectedtoxyz(context)) { networkutil.connecttoxyz(context); myapplication.setisreconnect(true); myapplication.setreconnectedcount(count++); } } catch (exception e) {e.printstacktrace();} } else { stopself(); } int ms_interval = 3000; objhandler.postdelayed(mtasks, ms_interval); } };//end runnable mtasks
the problem app : crashed device, seems eating memory ram. wifi xyz disconnect wont connect again , if user change wifi, won't allow connection.
i appreciate help. thank you.
check network connected name using
public string getwifiname(context context) { wifimanager manager = (wifimanager) context.getsystemservice(context.wifi_service); if (manager.iswifienabled()) { wifiinfo wifiinfo = manager.getconnectioninfo(); if (wifiinfo != null) { detailedstate state = wifiinfo.getdetailedstateof(wifiinfo.getsupplicantstate()); if (state == detailedstate.connected || state == detailedstate.obtaining_ipaddr) { return wifiinfo.getssid(); } } } return null; }
if name matches networkssid, i.e. xyz, resume service, else if doesn't match, stop service
if getwifiname(this).compareto("xyz") == 0 { //xyz network name on want resume service //code resume } else { //code stop service }
Comments
Post a Comment