Android 如何编写代码检查棒棒糖的互联网连接?
这是我用来检查internet连接的代码。除了棒棒糖,这对任何版本都很有效。我只是从几个链接中了解到,棒棒糖版本的URL类与我们在较低操作系统版本中使用的URL类不同。请任何人在这件事上帮我提供一个适用于所有版本手机的代码 代码是:Android 如何编写代码检查棒棒糖的互联网连接?,android,http,Android,Http,这是我用来检查internet连接的代码。除了棒棒糖,这对任何版本都很有效。我只是从几个链接中了解到,棒棒糖版本的URL类与我们在较低操作系统版本中使用的URL类不同。请任何人在这件事上帮我提供一个适用于所有版本手机的代码 代码是: @Override protected Boolean doInBackground(String... args){ ConnectivityManager cm = (ConnectivityManager) getSystemServ
@Override
protected Boolean doInBackground(String... args){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(4000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
要在每个版本的android中检查互联网连接,请尝试以下操作: 1-创建一个名为ConnectionDetector的类并复制此代码
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context) {
this._context = context;
}
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
}
在OnCreate方法中,无论您想在何处检查internet连接,请使用以下条件:
_cd = new ConnectionDetector(getApplicationContext());
_isInternetPresent = _cd.isConnectingToInternet();
if (_isInternetPresent) {
// your code that u want run if internet is connected
} else {
Toast.makeText(DisplayActivity.this, "Not communicate",Toast.LENGTH_SHORT).show();
}
简单实用…要在每个版本的android中检查互联网连接,请尝试以下方法: 1-创建一个名为ConnectionDetector的类并复制此代码
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context) {
this._context = context;
}
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
}
在OnCreate方法中,无论您想在何处检查internet连接,请使用以下条件:
_cd = new ConnectionDetector(getApplicationContext());
_isInternetPresent = _cd.isConnectingToInternet();
if (_isInternetPresent) {
// your code that u want run if internet is connected
} else {
Toast.makeText(DisplayActivity.this, "Not communicate",Toast.LENGTH_SHORT).show();
}
简单而有用…我遵循了这个链接
公共类连接检测器{
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
private Context\u Context;
公共连接检测器(上下文){
这._context=context;
}
公共布尔值未连接到Internet(){
ConnectivityManager connectivity=(ConnectivityManager)_context.getSystemService(context.connectivity_SERVICE);
if(连接性!=null)
{
NetworkInfo[]info=connectivity.getAllNetworkInfo();
如果(信息!=null)
对于(int i=0;i
}
创建该类的对象
ConnectionDetector cd=新的ConnectionDetector(上下文);
布尔值isInternetPresent=cd.isConnectingToInternet() 我跟踪了这个链接
公共类连接检测器{
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
private Context\u Context;
公共连接检测器(上下文){
这._context=context;
}
公共布尔值未连接到Internet(){
ConnectivityManager connectivity=(ConnectivityManager)_context.getSystemService(context.connectivity_SERVICE);
if(连接性!=null)
{
NetworkInfo[]info=connectivity.getAllNetworkInfo();
如果(信息!=null)
对于(int i=0;i
}
创建该类的对象
ConnectionDetector cd=新的ConnectionDetector(上下文);
布尔值isInternetPresent=cd.isConnectingToInternet() 用于检查internet连接
public static void isNetworkAvailable(final Handler handler, final int timeout) {
// ask fo message '0' (not connected) or '1' (connected) on 'handler'
// the answer must be send before before within the 'timeout' (in milliseconds)
new Thread() {
private boolean responded = false;
@Override
public void run() {
// set 'responded' to TRUE if is able to connect with google mobile (responds fast)
new Thread() {
URL aURL;
String link = "some link";
@Override
public void run() {
// HttpGet requestForTest = new HttpGet("https://lit-hamlet-6856.herokuapp.com/eventsList/TECHNICAL");
HttpURLConnection connection;
try {
aURL = new URL(link);
connection = (HttpURLConnection) aURL.openConnection();
connection.setRequestMethod("GET");
connection.connect();
// new DefaultHttpClient().execute(requestForTest); // can last...
if(connection.getResponseCode()==200)
responded = true;
Log.i(getClass().getName(),"RESULT");
}
catch (Exception e) {
}
}
}.start();
try {
int waited = 0;
while(!responded && (waited < timeout)) {
sleep(100);
if(!responded ) {
waited += 100;
}
}
}
catch(InterruptedException e) {} // do nothing
finally {
if (!responded) { handler.sendEmptyMessage(0); }
else { handler.sendEmptyMessage(1); }
}
}
}.start();
}
然后调用isNetworkAvailable(h,3000)代码>来自onCreate方法
请放心,这个类是有效的,因为我在运行5.1.1的Nexus4上使用这个代码来检查internet连接
public static void isNetworkAvailable(final Handler handler, final int timeout) {
// ask fo message '0' (not connected) or '1' (connected) on 'handler'
// the answer must be send before before within the 'timeout' (in milliseconds)
new Thread() {
private boolean responded = false;
@Override
public void run() {
// set 'responded' to TRUE if is able to connect with google mobile (responds fast)
new Thread() {
URL aURL;
String link = "some link";
@Override
public void run() {
// HttpGet requestForTest = new HttpGet("https://lit-hamlet-6856.herokuapp.com/eventsList/TECHNICAL");
HttpURLConnection connection;
try {
aURL = new URL(link);
connection = (HttpURLConnection) aURL.openConnection();
connection.setRequestMethod("GET");
connection.connect();
// new DefaultHttpClient().execute(requestForTest); // can last...
if(connection.getResponseCode()==200)
responded = true;
Log.i(getClass().getName(),"RESULT");
}
catch (Exception e) {
}
}
}.start();
try {
int waited = 0;
while(!responded && (waited < timeout)) {
sleep(100);
if(!responded ) {
waited += 100;
}
}
}
catch(InterruptedException e) {} // do nothing
finally {
if (!responded) { handler.sendEmptyMessage(0); }
else { handler.sendEmptyMessage(1); }
}
}
}.start();
}
然后调用isNetworkAvailable(h,3000)代码>来自onCreate方法
请放心,这个类是有效的,因为我在运行5.1.1的nexus 4上使用了这个代码