【问题标题】:How to check network connection within ViewGroup Fragment?如何检查 ViewGroup 片段中的网络连接?
【发布时间】:2016-05-24 08:31:37
【问题描述】:

所以我只是想检查我的网络连接,如果它是真或假,则执行执行。我知道在一个片段中你可以在获取系统服务之前使用 getactivity,但这对我不起作用?感谢您的帮助:)

public class Fragment1 extends Fragment implements View.OnClickListener {

TextView textView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag1, container, false);

ConnectivityManager connectivityManager =(ConnectivityManager).getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if(networkInfo!=null && networkInfo.isConnected())
        {
            textView.setVisibility(View.INVISIBLE);
        }
        else
        {
            Byron.setEnabled(false);
            Lennox.setEnabled(false);
            Skenners.setEnabled(false);
            Ballina.setEnabled(false);
        }

【问题讨论】:

    标签: java android android-fragments fragment


    【解决方案1】:

    在 ViewGroup 中可以使用 getContext() 方法获取系统服务。

    ConnectivityManager connectivityManager =(ConnectivityManager)getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    

    【讨论】:

      【解决方案2】:

      您可以在自定义视图/视图组的所有构造函数中添加一个 init() 函数,如下所示:

      public class CustomView extends Button {
          public CustomView(Context context) {
              super(context);
      
              init();
          }
      
          public CustomView(Context context, AttributeSet attrs) {
              super(context, attrs);
      
              init();
          }
      
          public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
              super(context, attrs, defStyleAttr);
      
              init();
          }
      
          public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
              super(context, attrs, defStyleAttr, defStyleRes);
      
              init();
          }
      
          private void init() {
              ConnectivityManager connectivityManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
              // Other stuffs
          }
      }
      

      【讨论】:

        【解决方案3】:

        像这样在您的项目 AppUtil 中创建一个单独的类:

        public class AppUtil{
        public static boolean isNetworkConnectionAvailable(Context ctx, boolean showDialog){
                ConnectivityManager manager =(ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
        
                boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
                if(!isConnected&&showDialog){
                    AppUtil.showSimpleDialog(ctx, ctx.getString(R.string.network_not_available),
                            ctx.getString(R.string.internet_not_available));
                }
                return isConnected;
            }
        
        }
        

        要检查网络连接,请按如下方式进行:

        public class Fragment1 extends Fragment implements View.OnClickListener {
        
        TextView textView;
        
        if (AppUtil.isNetworkConnectionAvailable(this, true)){
         textView.setVisibility(View.INVISIBLE);
        }
        else{
         Byron.setEnabled(false);
                    Lennox.setEnabled(false);
                    Skenners.setEnabled(false);
                    Ballina.setEnabled(false);
        }
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 2013-05-05
          • 2010-10-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-18
          相关资源
          最近更新 更多