how to check internet connection in asynctask in android

后端 未结 4 1002
一整个雨季
一整个雨季 2021-01-14 23:33
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        req         


        
相关标签:
4条回答
  • 2021-01-15 00:10
    if(CheckNetwork){
    new GetVideoUrlAsyn().execute();
    }else{
    // Check internet connection
    }
    

    and

    private boolean CheckNetwork() {
        ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conMgr.getActiveNetworkInfo() != null
                && conMgr.getActiveNetworkInfo().isAvailable()
                && conMgr.getActiveNetworkInfo().isConnected()) {
            return true;
        } else {
            return false;
        }
    
    }
    
    0 讨论(0)
  • 2021-01-15 00:12

    You should use a broadcast receiver as mentioned in other SO answers, but since you have asked how to do this otherwise, here you go:

     * Checks for an existing network connectivity
     * 
     * @param context
     *            The {@link Context} which is needed to tap
     *            {@link Context#CONNECTIVITY_SERVICE}
     * @return True if network connection is available
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
    
        if (connectivity == null) {
            return false;
        } else {
            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;
    }
    

    Will need additional permission to be specified in the manifest file

    0 讨论(0)
  • 2021-01-15 00:12

    check internet before execute asyncTask

    if(checkNetwork)
    {
    new GetVideoUrlAsyn().execute();    
    }
    else
    {
     // do stuff
    }
    
    0 讨论(0)
  • 2021-01-15 00:24

    Check network connectivity before executing AsyncTask,

    if(isNetworkAvailable(this))
    {
    new GetVideoUrlAsyn().execute();
    }
    

    and this is the method definition,

        public boolean isNetworkAvailable(Context ctx)
    {
        ConnectivityManager cm = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()&& cm.getActiveNetworkInfo().isAvailable()&& cm.getActiveNetworkInfo().isConnected()) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    

    do something like this inside your method callhttppost_numvaluepair(URL, nameValuePairs) of WebServiceCall class,

    HttpParams basicparams = new BasicHttpParams();
    URI uri = new URI(url);
    HttpPost method = new HttpPost(uri);
    int timeoutConnection = 60000;//set your timeout period
    HttpConnectionParams.setConnectionTimeout(basicparams,
            timeoutConnection);
    DefaultHttpClient client = new DefaultHttpClient(basicparams);
        //and then rest of your code
    

    Note: Don't forget to put the permission in manifest file,

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    
    0 讨论(0)
提交回复
热议问题