如何在 android 中检查我的 android 设备是不是有互联网连接? [复制]
Posted
技术标签:
【中文标题】如何在 android 中检查我的 android 设备是不是有互联网连接? [复制]【英文标题】:How can i check my android device having a internet connection in android? [duplicate]如何在 android 中检查我的 android 设备是否有互联网连接? [复制] 【发布时间】:2016-03-26 09:07:20 【问题描述】:说明: 我正在尝试连接具有 Internet 连接的设备。我在网络状态和 Internet 中添加了权限。我正确检查了我的设备是否与特定网络连接。例如无线网络。
我如何检查这个 wifi 连接共享互联网。
这是我的班级,有两种方法用于检查互联网连接和网络连接。
package comman;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import java.net.URL;
import java.io.IOException;
import java.net.HttpURLConnection;
public class ConnectionDetector
private Context _context;
public ConnectionDetector(Context context)
this._context = context;
public boolean isConnectingToInternet()
if (networkConnectivity())
try
HttpURLConnection urlc = (HttpURLConnection) (new URL(
"http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(3000);
urlc.setReadTimeout(4000);
urlc.connect();
// networkcode2 = urlc.getResponseCode();
return (urlc.getResponseCode() == 200);
catch (IOException e)
return (false);
else
return false;
private boolean networkConnectivity()
ConnectivityManager cm = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
return false;
这是我试图检查设备是否有互联网连接的代码?
ConnectionDetector cd=new ConnectionDetector(getContext());
if (cd.isConnectingToInternet())
new TabJson().execute();
else
dialog_popup();
这里是个例外
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfocom.angelnx.cricvilla.cricvilla/com.angelnx.cricvilla.cricvilla.MainActivity: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4950)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
at comman.ConnectionDetector.isConnectingToInternet(ConnectionDetector.java:29)
at com.angelnx.cricvilla.cricvilla.HomeFragment.onCreateView(HomeFragment.java:130)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
at android.app.Activity.performStart(Activity.java:5187)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4950)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
我该如何解决这个问题??? 请帮我解决这个问题。
【问题讨论】:
你读过异常吗?? 是的,我读到了一个例外 @Milan 使用单独的线程进行网络命中,您可以使用 AsyncTask 来完成。 好吧,你说得对,我是 android 新手,所以告诉我如何在 asynctask 中使用这个方法?? 【参考方案1】:抛出此异常是因为您在主线程中执行网络操作。在您的后台线程中执行此操作。即异步任务
class Task extends AsyncTask<String, void, boolean>
private Exception exception;
protected boolean doInBackground(String... xyz)
if (networkConnectivity())
try
HttpURLConnection urlc = (HttpURLConnection) (new URL(
"http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(3000);
urlc.setReadTimeout(4000);
urlc.connect();
// networkcode2 = urlc.getResponseCode();
return (urlc.getResponseCode() == 200);
catch (IOException e)
return (false);
else
return false;
private boolean networkConnectivity()
ConnectivityManager cm = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
return false;
【讨论】:
doInBackground 中只需要一个方法?? 什么意思?? 是的,我有两种方法,请查看我的帖子并告诉我需要更改的地方? 我的网络连接()放在哪里? 我已经编辑了我的代码!您在 doInBackground 中调用 networkconnectivty()。这应该可以正常工作【参考方案2】:这是因为您在主线程上调用 httprequest 。请改用AsyncTask。
【讨论】:
我如何使用异步任务来检查我的设备是否连接到互联网? 如果你不介意的话。你有时间给我解释一下吗?? 嘿,这个问题与您的连接检查无关。你的功能很好 如果有互联网连接,您必须将运行的代码放入异步任务。以上是关于如何在 android 中检查我的 android 设备是不是有互联网连接? [复制]的主要内容,如果未能解决你的问题,请参考以下文章