android - Error receiving broadcast intent wifi manager FTP Connection -


i'm coding android application supposed connect ftp server. i'm trying make ftp connection automatic. instance, if user connected wifi , disable wifi on phone, , enable later, want app automatically reconnect ftp server. that, i'm using broadcast receiver method onreceive called whenever there connectivity_action. here onreceive() method :

public void onreceive(context context, intent intent) {      if(intent == null)         return;     connectivitymanager manager = (connectivitymanager) context.getsystemservice(context.connectivity_service);     networkinfo ni = manager.getactivenetworkinfo();      wifimanager wifimgr = (wifimanager) context.getsystemservice(context.wifi_service);     if(wifimgr != null && wifimgr.iswifienabled()){         wifiinfo winfo = wifimgr.getconnectioninfo();         if(winfo.getnetworkid() == -1){             connected = false;         } else {             connected = true;         }     } else{         connected = false;     }     notifystatetoall(); } 

the notifystatetoall(); function active listeners calling function networkavailable() if connected, , networkunavailable() if not.

i have listener thread tries connect ftp server whenever connected true. here problem. when connected true & listener tries connect, error :

09-12 09:04:54.221 15834-15834/app.smartmonument.com.guide e/androidruntime: fatal exception: main      process: app.smartmonument.com.guide, pid: 15834      java.lang.runtimeexception: error receiving broadcast intent { act=android.net.conn.connectivity_change flg=0x4000010 bqhint=4 (has extras) } in app.smartmonument.com.guide.networkstatereceiver@fcd0242          @ android.app.loadedapk$receiverdispatcher$args.run(loadedapk.java:1003)          @ android.os.handler.handlecallback(handler.java:739)          @ android.os.handler.dispatchmessage(handler.java:95)          @ android.os.looper.loop(looper.java:158)          @ android.app.activitythread.main(activitythread.java:7229)          @ java.lang.reflect.method.invoke(native method)          @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230)          @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120)       caused by: android.os.networkonmainthreadexception          @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1273)          @ libcore.io.blockguardos.connect(blockguardos.java:110)          @ libcore.io.iobridge.connecterrno(iobridge.java:137)          @ libcore.io.iobridge.connect(iobridge.java:122)          @ java.net.plainsocketimpl.connect(plainsocketimpl.java:183)          @ java.net.plainsocketimpl.connect(plainsocketimpl.java:452)          @ java.net.socket.connect(socket.java:884)          @ org.apache.commons.net.socketclient._connect(socketclient.java:243)          @ org.apache.commons.net.socketclient.connect(socketclient.java:202)          @ app.smartmonument.com.guide.ftpcreator.connect(ftpcreator.java:76)          @ app.smartmonument.com.guide.ftpthread.networkavailable(ftpthread.java:81)          @ app.smartmonument.com.guide.networkstatereceiver.notifystate(networkstatereceiver.java:68)          @ app.smartmonument.com.guide.networkstatereceiver.notifystatetoall(networkstatereceiver.java:60)          @ app.smartmonument.com.guide.networkstatereceiver.onreceive(networkstatereceiver.java:55)          @ android.app.loadedapk$receiverdispatcher$args.run(loadedapk.java:993)          @ android.os.handler.handlecallback(handler.java:739)           @ android.os.handler.dispatchmessage(handler.java:95)           @ android.os.looper.loop(looper.java:158)           @ android.app.activitythread.main(activitythread.java:7229)           @ java.lang.reflect.method.invoke(native method)           @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230)           @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120)  

this error refers "ftp.connect()" of method called when "connected == true" : ftp object ftpclient() object.

@override public void networkavailable(){     try{         ftp.connect();         ftp.changedir("/eiffel");     } catch(ioexception e){         e.printstacktrace();     } } 


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -