【问题标题】:How to Call a method every time when Network is Available in Android?每次Android中网络可用时如何调用方法?
【发布时间】:2017-05-05 05:08:12
【问题描述】:

场景是当我的应用程序脱机时,我正在数据库中存储一些需要发送到服务器的信息。因此,当网络可用时,我想通过 Api 调用发送它们。有什么办法可以在每次网络可用的时候调用方法吗??

【问题讨论】:

标签: android android-volley job-queue


【解决方案1】:

创建广播接收器:

public class InternetBroadcastReceiver extends BroadcastReceiver {
static final String TAG = "InternetReceiver";
private static int networkType = -1;
private static boolean connectedWithNetwork = false;

public static void initNetworkStatus(Context context) {
    ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    networkType = -1;
    if (networkInfo != null) {
        Log.d(TAG, "Init: ACTIVE NetworkInfo: " + networkInfo.toString());
        if (networkInfo.isConnected()) {
            networkType = networkInfo.getType();
        }
    }
    Log.d(TAG, "initNetworkStatus -> " + networkType);
}

@Override
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "onReceive " + intent.getAction());

    try {
        if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
            Log.d(TAG, "System shutdown, stopping yaxim.");
        } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {


            ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

            boolean isConnected = (networkInfo != null) && (networkInfo.isConnected() == true);
            boolean wasConnected = (networkType != -1);

            if (connectedWithNetwork && isConnected)
                return;

            connectedWithNetwork = isConnected;

            if (wasConnected && !isConnected) {
                Log.d(TAG, "we got disconnected");
                networkType = -1;

            } else if (isConnected && (networkInfo.getType() != networkType)) {
                Log.d(TAG, "we got (re)connected: " + networkInfo.toString());
                networkType = networkInfo.getType();
                try {
                    Intent bIntent = new Intent("custom-event-name");
                    bIntent.putExtra("subject", Utils.SUBJECT_INTERNET_CONNECTED);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(bIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (isConnected && (networkInfo.getType() == networkType)) {
                Log.d(TAG, "we stay connected, sending a ping");
                try {
                    Intent bIntent = new Intent("custom-event-name");
                    bIntent.putExtra("subject", Utils.SUBJECT_INTERNET_CONNECTED);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(bIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else
                return;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

并在清单文件中注册

<receiver android:name=".packagename.InternetBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_SHUTDOWN" />
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>

【讨论】:

    【解决方案2】:
    /// BROADCAST RECEIVER CLASS
    
        public class UpdateReceiver extends BroadcastReceiver 
        {
    
    
    
            @Override
            public void onReceive(Context context, Intent arg1) 
        {
    
                boolean isConnected = arg1.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                if (isConnected) {
    
                } else {
    
                   //FETCH YOUR DATA FROM SQLITE
    
                   //CALL API
    
                }
            }
        }   
    
    
    
    /// ANDROID MANIFEST CODE
    
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
      <receiver android:name="com.example.welcome.nkew.UiUtils.UpdateReceiver">
                <intent-filter>
                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                </intent-filter>
            </receiver>
    

    【讨论】:

    • 感谢大家的回答。它对我帮助很大
    猜你喜欢
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 2021-04-09
    • 2014-07-03
    • 2021-01-03
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    相关资源
    最近更新 更多