Example: Android bi-directional network socket using AsyncTask

The SendDataToNetwork task runs in the main ui thread, meaning it will crash a Honeycomb or higher app due to NetworkOnMainThreadException Fatal exception. Here’s what my SendDataToNetwork looks like to avoid this issue: public boolean sendDataToNetwork(final byte[] cmd) { if (_nsocket.isConnected()) { Log.i(TAG, “SendDataToNetwork: Writing received message to socket”); new Thread(new Runnable() { public void … Read more

Intent action for network events in android sdk

Here’s a working example: <uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” /> <receiver android:name=”.receiver.ConnectivityReceiver”> <intent-filter> <action android:name=”android.net.conn.CONNECTIVITY_CHANGE” /> </intent-filter> </receiver> . public class ConnectivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d(ConnectivityReceiver.class.getSimpleName(), “action: ” + intent.getAction()); } }

Workaround mapping between NetworkInfo and NetworkInterface

Since API 21, you can use LinkProperties to get a NetworkInterface object out of a Network network object: ConnectivityManager manager = getSystemService(ConnectivityManager.class); LinkProperties prop = manager.getLinkProperties(network); NetworkInterface iface = NetworkInterface.getByName(prop.getInterfaceName()); However, although getAllNetworks() is supposed to replace getAllNetworkInfo() (the former, introduced in API 21, has deprecated the latter in API 23), as of Android 6.0.1, … Read more

Get my wifi ip address Android

So something to consider is that Formatter.formatIpAddress(int) is being deprecated: This method was deprecated in API level 12. Use getHostAddress(), which supports both IPv4 and IPv6 addresses. This method does not support IPv6 addresses. So using formatIpAddress(int) is likely not a good long term solution, although it will work. Here is a potential solution if … Read more

Volley and AsyncTask

You don’t need to run Volley request on AsyncTask. Why: They manage all network related task on separate thread. If you look closely at library project they did not picture the AsyncTask. But they intelligently handle all network related task efficiently. Check RequestQueue.java class in Volley’s main package here I am pasting java doc. /** … Read more

Network listener Android

New java class: public class ConnectionChangeReceiver extends BroadcastReceiver { @Override public void onReceive( Context context, Intent intent ) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_MOBILE ); if ( activeNetInfo != null ) { Toast.makeText( context, “Active Network Type : ” + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show(); } … Read more

How to determine if network type is 2G, 3G or 4G

You can put this following method directly in your Utility class: Kotlin: /** Usage: `networkTypeClass(telephonyManager.networkType)` */ fun networkTypeClass(networkType: Int): String { when (networkType) { TelephonyManager.NETWORK_TYPE_GPRS, TelephonyManager.NETWORK_TYPE_EDGE, TelephonyManager.NETWORK_TYPE_CDMA, TelephonyManager.NETWORK_TYPE_1xRTT, TelephonyManager.NETWORK_TYPE_IDEN, TelephonyManager.NETWORK_TYPE_GSM -> return “2G” TelephonyManager.NETWORK_TYPE_UMTS, TelephonyManager.NETWORK_TYPE_EVDO_0, TelephonyManager.NETWORK_TYPE_EVDO_A, TelephonyManager.NETWORK_TYPE_HSDPA, TelephonyManager.NETWORK_TYPE_HSUPA, TelephonyManager.NETWORK_TYPE_HSPA, TelephonyManager.NETWORK_TYPE_EVDO_B, TelephonyManager.NETWORK_TYPE_EHRPD, TelephonyManager.NETWORK_TYPE_HSPAP, TelephonyManager.NETWORK_TYPE_TD_SCDMA -> return “3G” TelephonyManager.NETWORK_TYPE_LTE -> return “4G” TelephonyManager.NETWORK_TYPE_NR -> return “5G” else … Read more

Get response status code using Retrofit 2.0 and RxJava

Instead of declaring the API call like you did: Observable<MyResponseObject> apiCall(@Body body); You can also declare it like this: Observable<Response<MyResponseObject>> apiCall(@Body body); You will then have a Subscriber like the following: new Subscriber<Response<StartupResponse>>() { @Override public void onCompleted() {} @Override public void onError(Throwable e) { Timber.e(e, “onError: %”, e.toString()); // network errors, e. g. UnknownHostException, … Read more

tech