在整个应用程序中全局广播和接收连接更改的最佳做法是什么?

时间:2015-09-09 08:57:56

标签: android notifications broadcastreceiver

我有这个简单的广播接收器正在观察数据连接的变化:

public class NetworkChangeReceiver extends BroadcastReceiver {

    private Context mContext;
    private Boolean mIsConnectedToInternet;
    private enum AlarmTypes {ALARM_TYPE_REPEATED, ALARM_TYPE_UNIQUE}

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            this.mContext = context;
            Logger.d("Connection changed!!!");
            triggerActionBasedOnTheConnectionType(isConnectedToInternet());
        } catch (Exception e) {
            Logger.e("onReceive method cannot be processed");
            e.printStackTrace();
        }
    }

    public boolean isConnectedToInternet() {
        mIsConnectedToInternet = false;
        try {
            ConnectivityManager cm =
                    (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            mIsConnectedToInternet = activeNetwork != null &&
                    activeNetwork.isConnectedOrConnecting();

        } catch (Exception e) {
            Logger.e("onReceive method cannot be processed");
            e.printStackTrace();
        }
        return mIsConnectedToInternet;
    }


    public void triggerActionBasedOnTheConnectionType(Boolean mIsConnectedToInternet) {
        try{
            if(mIsConnectedToInternet == true) {
                Logger.d("Device is connected");
            }
            if(mIsConnectedToInternet == false) {
                Logger.d("Device is not connected");

            }
        } catch(Exception e) {
            Logger.e("triggerActionBasedOnTheConnectionType method cannot be processed");
        }

    }
}

如果设备在所有视图中处于脱机状态,我想在操作栏下显示一些通知栏,并在全局范围内显示某些类别的所有按钮。

我怎样才能以正确的方式做到这一点?这个库有吗?

非常感谢任何建议。

1 个答案:

答案 0 :(得分:1)

它是一个简单的连接改变广播接收器

  1. AndroidManifest.xml 文件

    中注册广播接收器
    <receiver android:name=".NetworkChangeBR">
       <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
       </intent-filter>
    </receiver>
    
  2. 还定义了访问网络状态和拥有互联网的权限

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    
    1. 接下来,使用以下内容创建NetworkChangeBR.java文件

       @Override
       public void onReceive(final Context context, Intent intent) {
         int status = getConnectivityStatus(context);
         if(status==0){
           // Offline
           // Display notification
           // Deactivate buttons
         }else{
           // Connected 
           // Also you can check for status==ConnectivityManager.TYPE_WIFI or status==ConnectivityManager.TYPE_MOBILE
         }
      
       }
      
    2. 其中 getConnectivityStatus 函数定义为

      // Method to get status of network
      private int getConnectivityStatus(Context context) {
          ConnectivityManager cm = (ConnectivityManager) context
                  .getSystemService(Context.CONNECTIVITY_SERVICE);
      
          NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
          if (activeNetwork != null  && activeNetwork.isConnected()) {
              if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI || activeNetwork.getType() == ConnectivityManager.TYPE_WIMAX) {
                  Log.d(LOGTAG, "Wifi");
                  return ConnectivityManager.TYPE_WIFI;
              }
              if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                  Log.d(LOGTAG, "MOBILE");
                  return ConnectivityManager.TYPE_MOBILE;
              }
          }
          Log.d(LOGTAG, "Not Connected");
          return 0;
      }