Inflate a warning in the view from a BroadcastReceiver

0

I have an application where it is necessary to monitor, and warn the user when he is without an internet connection available. To solve this, I created a BroadcastReceiver and show a toast when this happens. But I need to update this class so that instead of showing a toast, a TextView will be on the screen informing the user that it is offline. My idea is to put a layout with this TextView, over the current layout on the user screen until a connection is available, but the problem is that I can not make this view appear on any screen the user is on.

My Bradcast code is now:

public class Connectivity extends BroadcastReceiver {

Util util = new Util();
private ViewGroup mLinearLayout;

@Override
public void onReceive(Context context, Intent intent) {
    if (!checkInternet(context)) {
        util.showToast(context, "Sem conexão disponível com a internet.", 1);
    }
}

boolean checkInternet(Context context) {
    ServiceManager serviceManager = new ServiceManager(context);
    if (serviceManager.isNetworkAvailable()) {
        return true;
    } else {
        return false;
    }
}

}

And the layout I'm trying to inflate, looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="Sem conexão com internet disponível."
    android:id="@+id/message"
    android:layout_gravity="center_horizontal"
    android:background="#e01b18"
    android:gravity="center"
    android:textColor="#FFF"
    android:textSize="16dp" />
</RelativeLayout>

How do I get a warning from a class, regardless of which screen it is on?

In the end I need you to look like this:

    
asked by anonymous 04.09.2015 / 16:26

1 answer

0

It was not the way I expected, but this was the best way I could find it. I used this post as a base: link

I registered a BroadcastReceiver within my activity:

IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
this.registerReceiver(Receiver, filter);

And I created the method that will be responsible for making the changes in the view as the connection changes happen:

private final BroadcastReceiver Receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(!util.verificaConexao(context))
            msgPopUp.setVisibility(View.VISIBLE);
        else
            msgPopUp.setVisibility(View.INVISIBLE);
    }
};

So the receiver works as it should and the messages in the view as well.

If you want to make it more organized, as is the case with me, you can create a class. With the code below I created a class that would be responsible for registering the broadcast and making the changes in the view

public class Broadcasts {
private TextView msgPopUp;
Util util = new Util();
View rootView;

public void registerBroadcastConnectivity(Context context, RelativeLayout mainLayout){
    IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
    context.registerReceiver(Receiver, filter);

    rootView = ((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content);

    LayoutInflater inflater =
            (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View menuLayout = inflater.inflate(R.layout.notificacao_internet, mainLayout, true);
}

private final BroadcastReceiver Receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(msgPopUp == null)
            msgPopUp = (TextView)rootView.findViewById(R.id.popUpConexao);

        if(!util.verificaConexao(context)) {
            msgPopUp.setVisibility(View.VISIBLE);
        }else {
            msgPopUp.setVisibility(View.INVISIBLE);
        }
    }
};

}

And below is the instance of the object, and the call to record the broadcast passing the context and layout of the activity

 Broadcasts broadcast = new Broadcasts();
    broadcast.registerBroadcastConnectivity(this, (RelativeLayout)findViewById(R.id.opcaoMesaLayout));

With the class I find more elegant and organized.

    
06.09.2015 / 16:21