I have BroadCastReceiver that control my connectivity. I would, when I lost connection, show message, but not generic toast, but a "layout" that I designed. my broadcast is a generic broadCast for connection:
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. ");
}
}
}
And my "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>
Is there a way for show this layout indistinctly current activity?
@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);
}
}