Inflate a warning in the view from a Broadcastreceiver

Asked

Viewed 379 times

0

I have an application where it is necessary to monitor, and warn the user when he is not available with the internet. To solve this, I created a Broadcastreceiver and show a Toast when this situation happens. But I need to update this class so that, instead of showing a Textview, a Textview is on the screen informing the user that it is not connected. My idea is to put a layout with this Textview, on the current layout on the user screen until a connection is available, but the problem is that I can’t get this view to be displayed on any screen that the user is.

My Bradcast code is like this at the moment:

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, it’s 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 can I, from a class, show this warning regardless of which screen it is on?

In the end I need you to stay like this:

Exemplo de como preciso que fique

  • 1

    One possibility is to use a Alertdialog.

  • The problem is that Alertdialog will not be on the screen forever, the user will close, and will not see the warning anymore. The idea is that the message stays on the screen, as well as the image I put in the edition.

  • 1

    To do this yes, there are several ways, one that I find very cool and useful to learn is using the Lib Event Bus, it creates communication between entities or your Broadasct for your Activity or service Fragment at the end, study anything I can post a code snippet for you as a response

  • Cool! I was a little lost in what to research to help solve this, with this tip will be quite useful. If you can post this code it will be perfect! Thank you.

1 answer

0


It wasn’t the way I expected, but this was the best way I could do it. I used this post as a basis: https://stackoverflow.com/questions/8766739/show-an-alert-dialog-in-broadcast-receiver-after-a-system-reboot

I registered a Broadcastreceiver inside 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 changes to 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 too.

If you want to make it more organized, as is my case, 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 by passing the context and layout of Activity

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

With the class I find more elegant and organized.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.