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:
One possibility is to use a Alertdialog.
– ramaral
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.
– Diego Neri
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
– Alessandro Barreto
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.
– Diego Neri