布局不会因网络状态更改而失效
Layout not invalidating upon network state change
我想在有 Internet 连接时在 activity 中显示名为 "content" 的 RelativeLayout,并在没有连接时将其替换为另一个名为 "noInternet" 的布局。
打开应用程序时应该实现相同的逻辑。
我关注了this, this and this
我创建了一个这样的网络接收器:
public class ConnectionChangeReceiver extends BroadcastReceiver {
static boolean connectivity;
@Override
public void onReceive(Context context, Intent intent )
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if ( activeNetworkInfo != null && activeNetworkInfo.isConnected() )
{
connectivity = true;
}
else {
connectivity = false;
}
}
public static boolean hasConnectivity(){
return connectivity;
}
}
在我的清单中,我在应用程序标签下添加了这个:
<receiver android:name="com.myPackage.ConnectionChangeReceiver"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
并在清单标签下:
<uses-permission android:name="android.permission.INTERNET" />
我的 activity 布局是这样的:
<RelativeLayout....>
....
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/noInternet"
android:visibility="gone">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/content">
...
</RelativeLayout>
为了切换布局,我在 activity 的 onCreate() 方法中编写了这段代码:
if(ConnectionChangeReceiver.hasConnectivity()){
noInternet.setVisibility(View.GONE);
content.setVisibility(View.VISIBLE);
} else {
content.setVisibility(View.GONE);
noInternet.setVisibility(View.VISIBLE);
}
问题是布局应该随着互联网连接的改变而切换,但实际上并没有。
我尝试了 invalidate()[参见 this] and handler [see this],但似乎没有任何效果。
我哪里错了?
在苦苦挣扎了将近一天之后,解决了我的问题:
我在 activity 中添加了以下代码:
@Override
public void onBackPressed(){
finish();
}
我想在有 Internet 连接时在 activity 中显示名为 "content" 的 RelativeLayout,并在没有连接时将其替换为另一个名为 "noInternet" 的布局。 打开应用程序时应该实现相同的逻辑。
我关注了this, this and this 我创建了一个这样的网络接收器:
public class ConnectionChangeReceiver extends BroadcastReceiver {
static boolean connectivity;
@Override
public void onReceive(Context context, Intent intent )
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if ( activeNetworkInfo != null && activeNetworkInfo.isConnected() )
{
connectivity = true;
}
else {
connectivity = false;
}
}
public static boolean hasConnectivity(){
return connectivity;
}
}
在我的清单中,我在应用程序标签下添加了这个:
<receiver android:name="com.myPackage.ConnectionChangeReceiver"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
并在清单标签下:
<uses-permission android:name="android.permission.INTERNET" />
我的 activity 布局是这样的:
<RelativeLayout....>
....
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/noInternet"
android:visibility="gone">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/content">
...
</RelativeLayout>
为了切换布局,我在 activity 的 onCreate() 方法中编写了这段代码:
if(ConnectionChangeReceiver.hasConnectivity()){
noInternet.setVisibility(View.GONE);
content.setVisibility(View.VISIBLE);
} else {
content.setVisibility(View.GONE);
noInternet.setVisibility(View.VISIBLE);
}
问题是布局应该随着互联网连接的改变而切换,但实际上并没有。 我尝试了 invalidate()[参见 this] and handler [see this],但似乎没有任何效果。
我哪里错了?
在苦苦挣扎了将近一天之后,解决了我的问题:
我在 activity 中添加了以下代码:
@Override
public void onBackPressed(){
finish();
}