Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的互联网状态检查_Android_Android Layout_Internet Connection - Fatal编程技术网

android中的互联网状态检查

android中的互联网状态检查,android,android-layout,internet-connection,Android,Android Layout,Internet Connection,在我的android应用程序中,我使用了大量的AsyncTasks。我想在运行每个异步任务时检查internet连接。我可以创建一个公共类和公共xml布局来显示“无连接”吗与其检查每个AsyncTasks执行情况?只需创建一个错误布局并将其包含到每个xml中,或者在运行时按您的意愿添加它 <include android:id="@+id/error_view" layout="@layout/view_error" android:layout_width="m

在我的android应用程序中,我使用了大量的
AsyncTasks
。我想在运行每个异步任务时检查internet连接。我可以创建一个公共类和公共
xml
布局来显示“无连接”吗与其检查每个
AsyncTasks
执行情况?

只需创建一个错误布局并将其包含到每个xml中,或者在运行时按您的意愿添加它

 <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" />

为什么你想在asyntask中使用它,你可以使用广播接收器,它不会阻止主ui线程,那么在运行asyntask时如何知道网络是否存在?而是为它创建单独的布局,只需尝试在AlertDialog上显示。@Aswinnair检查此ans@Nilu谢谢,我会尝试此方法
 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;
}