尝试使用BroadcastReceiver在Android中检测Internet连接

时间:2017-04-08 07:52:34

标签: android android-fragments broadcastreceiver

我正在尝试使用BroadcastReciver检测Internet连接。我编写了所有代码并且它在活动中工作正常但是当我在Fragment中测试这段代码时,即使onNetworkConnectionChanged()在onResume()之后也没有被调用

这是我的以下带有代码的类

  
      
  1. 这是AppController.java文件的代码
  2.   
public class AppController extends Application {
    static AppController mInstance;
    private static final String TAG = "AppController";

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
        ConnectivityReceiver.connectivityReceiverListener = listener;
    }

    public void showSnackBar(boolean isConnected, View view) {
        String msg = "";
        if (isConnected) {
            Log.e(TAG, "showSnackBar: connected with internet ");
        } else {
            msg = "Something went wrong, Please check your Internet Connetion!";
            Log.e(TAG, "showSnackBar: no internet connection");
        }
        Snackbar snackbar = Snackbar.make(view, msg, Snackbar.LENGTH_INDEFINITE);
        snackbar.setAction("OK", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        snackbar.setActionTextColor(Color.WHITE);
        View view1 = snackbar.getView();
        TextView textView = (TextView) view1.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.RED);
        snackbar.show();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }
}
  
      
  1. 这是ConnectivityReceiver.java文件的代码
  2.   
public class ConnectivityReceiver extends BroadcastReceiver {
    public static ConnectivityReceiverListener connectivityReceiverListener;

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        boolean isCOnnected = networkInfo != null && networkInfo.isConnectedOrConnecting();
        if (connectivityReceiverListener != null) {
            connectivityReceiverListener.onNetworkConnectionChanged(isCOnnected);
        }
    }

    public static boolean isConnected() {
        ConnectivityManager cm = (ConnectivityManager) AppController.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        boolean isConnected = networkInfo != null && networkInfo.isConnectedOrConnecting();
        return isConnected;
    }

    public interface ConnectivityReceiverListener {
        void onNetworkConnectionChanged(boolean isConnected);
    }
}
  
      
  1. 这是CustomerService.java文件的代码(这是片段)
  2.   
public class CustomerService extends Fragment implements ConnectivityReceiver.ConnectivityReceiverListener {
    private static final String TAG = "CustomerService";
    LinearLayout linearLayout;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.layoyt_customer_service, container, false);
        linearLayout=(LinearLayout)view.findViewById(R.id.linear_layout_customer);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: Calling");
        AppController.getInstance().setConnectivityListener(this );
    }

    @Override
    public void onNetworkConnectionChanged(boolean isConnected) {
        Log.e(TAG, "onNetworkConnectionChanged: " );
        AppController.getInstance().showSnackBar(isConnected, linearLayout);
    }
}
  

4.最后这是layout_customer_service.xml文件的代码

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linear_layout_customer"
        android:orientation="vertical">

    </LinearLayout>
  

注意:我已从活动中调用此片段

正如我之前提到的,它在活动中工作正常,但没有在片段中工作。谢谢.....

0 个答案:

没有答案