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?
Asked
Active
Viewed 41 times
-4

AskNilesh
- 67,701
- 16
- 123
- 163

Aswin N Nair
- 67
- 1
- 9
-
why do u want it in asyntask , u can use broadcast reciever which wont block main ui thread – Quick learner Nov 03 '17 at 05:11
-
then how to know is net present or not while running asyntask..? – Aswin N Nair Nov 03 '17 at 05:20
-
Instead create separate layout for it just try to display on AlertDialog. – Haresh Chhelana Nov 03 '17 at 05:21
-
@AswinNNair check this ans https://stackoverflow.com/a/46986588/7666442 – AskNilesh Nov 03 '17 at 05:21
-
@Nilu thanks i will try this – Aswin N Nair Nov 03 '17 at 05:29
-
welcome @AswinNNair happy to help you let me know if any query – AskNilesh Nov 03 '17 at 05:30
1 Answers
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