-4

In my android application i am using lot of AsyncTasks.I want to check internet connection while running each Async Tasks.Can i create a common class and common xml layout for displaying "No connection" rather than checking each AsyncTasks execution?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Aswin N Nair
  • 67
  • 1
  • 9

1 Answers1

0

Just create a error layout and include it to each xml .Or add it at runtime as you wish to do .

 <include
    android:id="@+id/error_view"
    layout="@layout/view_error"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:visibility="gone" />

On any connection error make the error visible with specified message.There can be a retry button too. Toggle the visibility of error view according to connection available. Checking network connection :-

 public static boolean isNetworkConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return true;
        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return true;
    }
    return false;
}
ADM
  • 20,406
  • 11
  • 52
  • 83