我想檢查與廣播接收器的互聯網連接。我寫了一些代碼來檢查互聯網連接。以下是我的源代碼的Android檢查互聯網連接刷新當前片段
public static boolean isNetworkAvailable(Context context) {
boolean isMobile = false, isWifi = false;
NetworkInfo[] infoAvailableNetworks = getConnectivityManagerInstance(
context).getAllNetworkInfo();
if (infoAvailableNetworks != null) {
for (NetworkInfo network : infoAvailableNetworks) {
if (network.getType() == ConnectivityManager.TYPE_WIFI) {
if (network.isConnected() && network.isAvailable())
isWifi = true;
}
if (network.getType() == ConnectivityManager.TYPE_MOBILE) {
if (network.isConnected() && network.isAvailable())
isMobile = true;
}
}
}
return isMobile || isWifi;
}
@Override
public void onReceive(Context context, Intent intent) {
if (isNetworkAvailable(context))
{
Toast.makeText(context, "Network Available Do operations",Toast.LENGTH_LONG).show();
}
}
現在我想在片段中使用我的BroadcastReceiver。當互聯網連接不可用時,我試圖刷新片段。我不知道如何刷新我的片段。 如果有人知道解決方案,請幫助我 謝謝
如果要重裝你的'Fragment'顯示的數據,你可以使用'Loader'在片段加載數據和加載器將包括將在調用'onContentChange()'觸發重載的廣播接收器。 – BladeCoder
我明白如何重新加載片段,但告訴我我怎麼能在片段中調用我的onReceive方法? @Tauqir –