网络连接不在片段中工作

时间:2015-12-29 11:46:35

标签: android fragment internet-connection

在提出这个问题之前,我曾在Google&所以。但是没有希望。

  • 问题已连接互联网缺席时显示的Toast。

这是我的代码。我需要你的敏锐建议。

protected boolean isInternetOn() {

    ConnectivityManager connec = (ConnectivityManager)getActivity().getSystemService(getActivity().CONNECTIVITY_SERVICE);

    //check for network connections
    if(connec.getNetworkInfo(0).getState()== android.net.NetworkInfo.State.CONNECTED||
            connec.getNetworkInfo(0).getState()== android.net.NetworkInfo.State.CONNECTING||
            connec.getNetworkInfo(1).getState()== android.net.NetworkInfo.State.CONNECTING||
            connec.getNetworkInfo(1).getState()== android.net.NetworkInfo.State.CONNECTED){

        Toast.makeText(getActivity(), "Connected", Toast.LENGTH_LONG).show();
        return true;
    }else if (connec.getNetworkInfo(0).getState()== android.net.NetworkInfo.State.DISCONNECTED||
            connec.getNetworkInfo(1).getState()== android.net.NetworkInfo.State.DISCONNECTED){
        Toast.makeText(getActivity(), "Not Connected", Toast.LENGTH_LONG).show();
        return false;
    }
    return false;
}

现在在我的isInternetOn类中调用此abstract方法扩展Fragment

这是结构

  public abstract class HomeTabFragment extends Fragment
    //
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        if (RootView == null)
        {
        isInternetOn();
         }
    }
 Here return view//

我试过了,

  ConnectivityManager connec = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

有没有人遇到这个问题?非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

试试这个:

 ConnectivityManager connec = (ConnectivityManager)getActivity().getSystemService(getActivity().CONNECTIVITY_SERVICE);

更改为

 ConnectivityManager connec = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

答案 1 :(得分:0)

尝试这种方式。可能会帮到你

public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) getActivity().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;
    }

片段类

if (RootView!= null)
{
  isConnectingToInternet();
}

答案 2 :(得分:0)

You can write one class for check internet connection:

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

And After that you can check connection in your fragment by using below code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        cd = new ConnectionDetector(getActivity());
        isInternetPresent = cd.isConnectingToInternet();

        if (isInternetPresent) {
            // Internet Connection is Present
            Toast.makeText(getActivity(), "Connected", Toast.LENGTH_LONG).show();
        } else {
            // Internet connection is not present
            Toast.makeText(getActivity(), "Not Connected!", Toast.LENGTH_LONG).show();
        }

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }