2
The thing is, I’m creating an app that depends on the internet connection for most services. To make it more dynamic, I thought I’d do something similar to the Youtube app. In the youtube app it warns almost instantly that the app is disconnected. See:
I did a general search and tried to make use of Threads
in two ways:
1. Executor + Runnable
class ThreadInfinity implements Executor {
@Override
public void execute(@NonNull Runnable r) {
while (true) {
r.run();
}
}
}
Thread r = new Thread(new Runnable() {
@Override
public void run() {
if (manager.getActiveNetworkInfo() != null && manager.getActiveNetworkInfo().isConnectedOrConnecting()) {
Toast.makeText(getApplicationContext(), "Você está conectado", Toast.LENGTH_SHORT);
} else {
Toast.makeText(getApplicationContext(), "Você está desconectado", Toast.LENGTH_SHORT);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
ThreadInfinity t = new ThreadInfinity();
t.execute(r);
2. Asynctask
Context context = getApplicationContext();
final ConnectivityManager manager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
class TheadInfinity extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
while (true)
try {
if (manager.getActiveNetworkInfo() != null && manager.getActiveNetworkInfo().isConnectedOrConnecting()) {
Toast.makeText(getApplicationContext(), "Você está conectado", Toast.LENGTH_SHORT);
} else {
Toast.makeText(getApplicationContext(), "Você está desconectado", Toast.LENGTH_SHORT);
}
Thread.sleep(1000);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Erro", Toast.LENGTH_SHORT);
}
}
}
TheadInfinity t = new TheadInfinity();
t.doInBackground();
Both do not lock the application but the screen is blank. So:
I believe the correct mode is not with Thread. There must be some Systems that can be used. However I have not found anywhere "on the internet".
Doubt
How can I do that? =)
Dude, the android has the
Broadcasts
, which are notifications that the operating system itself for some events. In your case, I believe you should use the Intent filter for the actionCONNECTIVITY_CHANGE
. In the OR you already have an answer about that. If I’m not mistaken, there were changes in Android 8 and some of these events are no longer available, worth consult.– Diego Rafael Souza
@Diegorafaelsouza I’ll take a look.. Thank you.
– Andrei Coelho
@Diegorafaelsouza took a look. And it looks like that’s it. I’m gonna run some tests tomorrow. Anyway, it would be nice to formulate an answer here about it. Since it could help anyone looking for it. I didn’t even know it existed. =)
– Andrei Coelho
It really is a very interesting piece of content. But unfortunately I don’t know [tag:java] or native development [tag:android] to post something really useful without spending more time than I’d like. The tip is for those who have more availability or yourself, if you find your way using this solution =)
– Diego Rafael Souza
@Diegorafaelsouza I do not know. kkkk we will leave to the experts then.
– Andrei Coelho