我有BroadCastReceiver来控制我的连接。 当我失去连接时,我会显示消息,但不是通用的吐司,而是我设计的“布局”。 我的广播是用于连接的通用broadCast:
public class ConnectivityChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
ConnectivityManager cm =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo()!=null){
Toast.makeText(context, "Connected to Internet", Toast.LENGTH_LONG).show();
}
else{
/** HERE INSTEAD OF TOAST, IWOULD DISPLAY MESSAGE ON ACTIVITY **/
Toast.makeText(context, "not connection", Toast.LENGTH_LONG).show();
Log.i("INTERNET", "---------------------> Internet Disconnected. ");
}
}
}
我的“no_connection_layout”
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="52dp"
android:text="No InterNeT"
android:id="@+id/no_connection"
android:layout_weight="1"
android:layout_gravity="center|top"
android:gravity="center"
android:background="@android:color/holo_green_dark"
android:textStyle="bold"
android:textColor="#ffffff"
/>
</FrameLayout>
有没有办法让这种布局显得格外活跃?
答案 0 :(得分:0)
@Override
public void onReceive(final Context context, final Intent intent) {
//Obviously cleanup where you do this stuff, to make it efficient
//If your context is a fragment you need to cast to fragment and then do getActivity instead
final View noConnectionBanner = ((Activity) context).findViewById(R.id.no_connection)
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null) {
noConnectionBanner.setVisibility(View.GONE);
} else {
noConnectionBanner.setVisibility(View.VISIBLE);
}
}
答案 1 :(得分:0)
只需触发一个函数并编写您想要显示的视图元素。
public class ConnectionBroadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
} else {
Visibler();
}
}
}
@Override
protected void onResume() {
this.connectionBroadReceiver = new ConnectionBroadReceiver();
registerReceiver(this.connectionBroadReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(this.connectionBroadReceiver);
super.onPause();
}
然后在主程序中,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
floatingActionButton = (FloatingActionButton) findViewById(R.id.floater);
}
private void Visibler() {
floatingActionButton.setVisibility(View.VISIBLE);
}
答案 2 :(得分:-1)
创建全局布尔值,并在有互联网连接时将其设为true,在未连接时为false。 然后创建一个异步任务类doinBackground方法并在变量更改时连续检查它,并且从postExecute方法可以在UI中进行更改。如果你需要更多的帮助我。