Posted on

The Evolution of Android Network Access

Modern background execution in Android [https://android-developers.googleblog.com/2018/10/modern-background-execution-in-android.html]


https://medium.com/@elye.project/the-evolution-of-android-network-access-1e199fc6e9a2

  1. Dinosaur Age — Direct Access
  2. Stone Age — Using Thread
  3. Bronze Age — AsyncTask
  4. Middle Age — Intent Service
  5. Industrial Age — RxJava
  6. Current Time — Rxjava in Kotlin
  7. Future — Coroutines

Android background in nutshell (https://proandroiddev.com/android-background-in-a-nutshell-part-1-c29a5e0ed10e)

Руководство по фоновой работе в Android. Часть 1 (https://habrahabr.ru/company/epam_systems/blog/348894/)

Руководство по фоновой работе в Android. Часть 2: Loaders (https://habrahabr.ru/company/epam_systems/blog/350094/)(https://proandroiddev.com/android-background-in-a-nutshell-part-ii-loaders-f763f70fdd15)

Руководство по фоновой работе в Android. Часть 3: Executors и EventBus (https://habrahabr.ru/company/epam_systems/blog/351166/) – Android background in a nutshell — Part III. Executors and EventBus.(https://proandroiddev.com/android-background-in-a-nutshell-part-iii-executors-and-eventbus-43576bda47e9)

Руководство по фоновой работе в Android. Часть 4: RxJava (https://habrahabr.ru/company/epam_systems/blog/353852/) – Android background in a nutshell. Part 4 — RxJava (https://proandroiddev.com/android-background-in-a-nutshell-part-4-rxjava-a41c16985d71)

Руководство по фоновой работе в Android. Часть 5: Корутины в Котлине [https://habr.com/company/epam_systems/blog/415335/] – Android Background in a nutshell — Part V. Kotlin Coroutines. [https://proandroiddev.com/android-background-in-a-nutshell-part-v-kotlin-coroutines-f1bde21875f6]

Making Android Networking Pretty with Kotlin Coroutines (https://medium.cobeisfresh.com/making-android-networking-pretty-with-kotlin-coroutines-b364ffbd803c)

Posted on

LinkifyCompat, get Urls

import android.support.v4.text.util.LinkifyCompat;

@Nullable
    public void find(@NonNull String text) {
        List<String> urls = getLinkifiedUrls(text);
        for (String url : urls) {
          //
        }
    }

    private List<String> getLinkifiedUrls(String text) {
        SpannableString ss = new SpannableString(text);
        LinkifyCompat.addLinks(ss, Linkify.WEB_URLS);
        URLSpan[] urls = ss.getSpans(0, ss.length(), URLSpan.class);
        ArrayList<String> result = new ArrayList<>();
        for (URLSpan url : urls) {
            result.add(url.getURL());
        }
        return result;
    }
Posted on

Connection Detection Using LiveData — Android

https://android.jlelse.eu/connection-detection-using-livedata-android-623bd02b0e30

public class ConnectionModel {

    private int type;
    private boolean isConnected;

    public ConnectionModel(int type, boolean isConnected) {
        this.type = type;
        this.isConnected = isConnected;
    }

    public int getType() {
        return type;
    }

    public boolean getIsConnected() {
        return isConnected;
    }
}
public class ConnectionLiveData extends LiveData<ConnectionModel> {

    private Context context;

    public ConnectionLiveData(Context context) {
        this.context = context;
    }

    @Override
    protected void onActive() {
        super.onActive();
        IntentFilter filter = new    IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(networkReceiver, filter);
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        context.unregisterReceiver(networkReceiver);
    }

    private BroadcastReceiver networkReceiver = new BroadcastReceiver() {
        @SuppressWarnings("deprecation")
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getExtras()!=null) {
                NetworkInfo activeNetwork = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
                boolean isConnected = activeNetwork != null &&
                        activeNetwork.isConnectedOrConnecting();
                if(isConnected) {
                    switch (activeNetwork.getType()){
                        case ConnectivityManager.TYPE_WIFI:
                            postValue(new ConnectionModel(WifiData,true));
                            break;
                        case ConnectivityManager.TYPE_MOBILE:
                            postValue(new ConnectionModel(MobileData,true));
                            break;
                    }
                } else {
                    postValue(new ConnectionModel(0,false));
                }
            }
        }
    };
}
ConnectionLiveData connectionLiveData = new ConnectionLiveData(getApplicationContext());
connectionLiveData.observe(this, new Observer<ConnectionModel>() {
    @Override
    public void onChanged(@Nullable ConnectionModel connection) {
        if (connection.getIsConnected()) {
          switch (connection.getType()) {
            case WifiData:
            Toast.makeText(this, String.format("Wifi turned ON"),         Toast.LENGTH_SHORT).show();
            break;
            case MobileData:
            Toast.makeText(this, String.format("Mobile data turned ON"), Toast.LENGTH_SHORT).show();
            break;
          }
         } else {
            Toast.makeText(this, String.format("Connection turned OFF"), Toast.LENGTH_SHORT).show();
     }
    }
});