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

Android 在整个应用程序中广播和接收连接更改的最佳做法是什么?,android,notifications,broadcastreceiver,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 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");
        }

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

我怎样才能用正确的方法来做呢?这里有图书馆吗


非常感谢你的建议

这是一个简单的连接更改广播接收器

  • 在您的AndroidManifest.xml文件中注册广播接收器

    <receiver android:name=".NetworkChangeBR">
       <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
       </intent-filter>
    </receiver>
    
  • 其中,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;
    }
    

    谢谢,但我知道怎么用。我问过如何全局广播连接状态以处理UI更改(停用按钮、在actionbar下显示消息等),我不明白…一旦广播接收器检测到更改,基本上,您可以调用一个函数来停用按钮。这可能是一个静态函数或一个单例类。如果您在访问Ui元素方面遇到问题,那么一种方法可能是在活动中定义广播接收器。在这种情况下,您将引用Ui元素,您可以在脱机时根据我的评论停用它们..或显示通知。。
     @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
       }
    
     }
    
    // 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;
    }