什么是最好的方式来连续检查互联网连接android

什么是最好的方式来连续检查互联网连接android

问题描述:

我正在开发一个应用程序。在那个屏幕上检查Internet连接后,立即在onCreate()方法之后。如果网络连接是好的,我打电话给一个AsyncTask类用于加载国家列表,并在spinnerView的屏幕上显示它。如果没有网络连接,我将显示Toast Message to User并呼叫check_Network(AsyncTask)。在这个类保护龙doInBackground(URL ... params)方法我检查网络连接与否如果连接调用国家AsyncTask,否则我再次调用check_Network(AsyncTask)。这个过程重复,直到网络连接。我的问题是重复检查网络是正确的方法。请给我建议。对不起,我的英语很差,请understand.blow我显示我的代码什么是最好的方式来连续检查互联网连接android

if (CheckNetwork.isOnline(this)) { 
      try { 
       new CountryProcess().execute(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } else { 


      Toast.makeText(
        getApplicationContext(), 
        getString(R.string.network_connection_fail) 
          + "!", Toast.LENGTH_LONG).show(); 
      new NetWork_connectivity().execute(); 
} 

//.......................// 

class NetWork_connectivity extends AsyncTask<URL, Integer,Long> 
    { 
     @Override 
     protected Long doInBackground(URL... params) 
     { 
      if (CheckNetwork.isOnline(MainActivity.this)) { 

       new CountryProcess().execute(); 

      }else 
      { 
       new NetWork_connectivity().execute(); 
      } 

      return null; 
     } 
    } 
+0

为什么不干脆让国家列表RPC通过,然后上报,如果它的祝酒失败? – moofins

+0

我很长一段时间呆在那个屏幕上,所以我又怎么能打电话给CountryProcess异步类 –

添加下面的代码清单中,用于连接变化意图

<receiver android:name=".NetworkStateReceiver"> 
    <intent-filter> 
     <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
    </intent-filter> 
</receiver> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

而在接收端,得到额外添加相关的接收器意图并检查状态。所以每当网络状态发生变化时,您都会收到通知,然后相应地执行您的任务。

public class NetworkStateReceiver extends BroadcastReceiver { 
public void onReceive(Context context, Intent intent) { 
super.onReceive(context, intent); 
if(intent.getExtras()!=null) { 
    NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO); 
    if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) { 
     //connected 
    } 
} 
if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) { 
     //not connected 
} 
} 
} 

对于您的情况,您希望在清单中添加权限并在您的活动中添加注册接收方。

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); 
    registerReceiver(networkReceiver, filter); 

确保离开活动与

unregisterReceiver(networkReceiver); 

private BroadcastReceiver networkReceiver = new BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    super.onReceive(context, intent); 
    if(intent.getExtras()!=null) { 
     NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO); 
     if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) { 
      //connected 
     } 
    } 
    //not connected 
    } 
} 

并根据您的要求,您需要连接的状态只有之前的一次注销它。首先检查连通性,如果没有连接,那么只有注册接收机。

public boolean isNetworkConnected() { 
     ConnectivityManager cm = 
      (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
     if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
      return true; 
     } 
     return false; 
    } 
+0

谢谢,Anupam是最好的方法。 –

要访问互联网,我们需要Internet权限
为了检测我们需要ACCESS_NETWORK_STATE权限

在AndroidManifest.xml中添加这些行网络状态:

<!-- Internet Permissions --> 
    <uses-permission android:name="android.permission.INTERNET" /> 

<!-- Network State Permissions --> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

创建此方法在你的Java类中:

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; 
    } 

当过要检查网络状态在您的应用程序调用isConnectingToInternet()功能,它会返回true或false

ConnectionDetector cd = new ConnectionDetector(getApplicationContext()); 

Boolean isInternetPresent = cd.isConnectingToInternet(); // true or false