My app is running in the background (as a Service) and needs to be connected to a remote server all the time to receive and send messages.
This Service runs a Thread which manage the connection to the server (with Socket)
i'm trying to write an internet connection Broadcast Receiver which will run every time the internet state is changed and check if the state is connected of disconnected.
my problem is as follow: when i'm connected to Wi-Fi network the Broadcast Receiver Intent is fired few times which in all of them the state of the internet connection is true (with no disconnection between every fire, which means i have few new Threads using the Socket to connect the server.
How can i make sure i'm getting the state of the internet right, and by right i mean that the .isConnected() method will return if and only if the connection is connected ?
this is my code:
public void onReceive(Context context, Intent intent)
{
action = intent.getAction();
app = (AppVariables) context.getApplicationContext();
if (app.isApplicationInitilized())
{
if (action.equals(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)))
{
networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected())
app.getServerConnectionManager().startServer();
}
else if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
{
networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI && !networkInfo.isConnected())
app.getServerConnectionManager().stopServer(false);
else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE && !networkInfo.isConnected())
app.getServerConnectionManager().stopServer(false);
else
app.getServerConnectionManager().startServer();
}
}
}
I've had the same issue for one of my apps, and the pattern we deployed (successfully) is as follows:
onReceive():
if( networkInfo.isConnected() )
fireTimerForSecondaryCheck();
else
not connected;
SecondaryCheck:
if( loadKnownUrl() )
is connected.
fire long timer for secondary check
else
fire short timer for secondary check
Basically what this does is attempt to access the internet once the network manager reports that wifi is up. If it receives the expected content, then the network connection is up and working, and I'll check again in two minutes. If I don't receive the expected content, it means either the network connection is not fully up, can't connect to the internet, or the wifi might have a landing page for TOS or similar. In that case, I check again in 10 seconds.
FWIW, my NetworkMonitorService broadcasts connectivity to my app, which registers for it, and updates the UI as necessary.
This was developed in 2.3.3, and I believe the newer Android versions (4+) have finer granularity in the network state, but I haven't seen the need to adjust this code yet in production. It works well from 2.2 devices all the way to 4.2.
If I understand correctly you need to know the state of connection.
You can obtain that by using networkInfo state:
State state = networkInfo.getState();
if (state == State.CONNECTED) do_something
Related
I am developing the android app which runs my web service at app startup. My app is basically the home app of the device. So that's mean that When user will reboot the device, My app will come in front of him.
So far that is great. But I have noticed it that on app start the device takes 5 to 10 seconds to get the internet connected. whereas my web service gets run before the internet is established thus throwing an error of "An error occurred please try again later."
What I Want: Though I am showing that message I am really not satisfied as I really do want to show the user a Message. That message should be "Device is connecting to the internet"
but unfortunately, I am unable to do so. I am using the following code
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}
but it really indicates me if the net is connected or not.
So is not there the case where I can show a message like "Please wait device is connecting to the internet"
Or how can I get that device is restarted not the only app. ... Please help
Register a local (or Manifest-declared if you want) broadcast receiver in your activity (documentation here https://developer.android.com/guide/components/broadcasts), listening to connectivity changes, using this intent filter: IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION).
Then check if is the device has internet connection with the code you posted.
Remember to register your broadcast receiver inside onResume() method and to unregister it inside onPause() method
From api 24 and above:
you can use registerDefaultNetworkCallback https://developer.android.com/reference/android/net/ConnectivityManager.html#registerDefaultNetworkCallback(android.net.ConnectivityManager.NetworkCallback)
The callback has a public method called "onAvailable" https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback.html#onAvailable(android.net.Network) that is called when a new network is ready to be used.
I am trying to connect to a WiFi network using WiFiManager. Here is the code I use to do so :
WifiManager manager = [...];
WifiConfiguration config = new WifiConfiguration();
manager.setWifiEnabled(true);
//Set SSID, PSK, etc...
[...]
//Connect to the network
int netId = manager.addNetwork(config);
if (netId == -1)
{
Toast.makeText(this, "Invalid PSK", Toast.LENGTH_LONG).show();
return;
}
manager.enableNetwork(netId, true);
manager.saveConfiguration();
Beforehand, I registered a BroadcastReceiver for the WifiManager.NETWORK_STATE_CHANGED_ACTION action. I'm trying to detect when the connection succeeds or fails (for various reasons including authentication) by checking the DetailedState of the given NetworkInfo.
Everything works when the connection succeeds, but for some reason my receiver isn't "executed" when the connection fails. The network is added to the device's list, I can see it by going into WiFi Settings, but it does not try to connect (or it does try to connect but doesn't notify my receiver).
Any clue ? Thanks !
I finally managed to achieve what I wanted.
To detect that the connection succeeded, register the WifiManager.NETWORK_STATE_CHANGED_ACTION broadcast intent and check for the NetworkInfo.DetailedState.OBTAINING_IPADDR detailed state (for WiFi obviously).
To detect that the connection failed, register the WifiManager.SUPPLICANT_STATE_CHANGED_ACTION broadcast intent and check for any WifiManager.EXTRA_SUPPLICANT_ERROR integer extra (typically WifiManager.ERROR_AUTHENTICATING). You may need to gain access for Android hidden API to achieve that.
The enableNetwork method return true if the connection status is success.
This works fine on success but is not the best method to check the failed status (return true if the connection failed after the first step authentication).
I think that you can check in the networkInfo object the actual connected network to see if the request is ok:
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
While working on a little project that integrates with GCM, I've stumbled across a bit of a strange issue.
Some times when I start watching the log to see if messages are received, messages do not appear to be coming through until I have changed the network state (I.E. originally on WiFi, if I turn off WiFi and move to Mobile Data, the messages arrive fine). After I've changed the network state, messages start to arrive perfectly fine, and the same applies once I change the network state back to what it was before (in this case, WiFi) the messages continue to be received.
The project itself includes the ability to start on boot (starts the GCMBaseIntentService on boot), which again works perfectly fine, and I'm sure the app / service is running as I've manually started up the app when this issue occurs (which also checks to see if the service is running, and if it's not it runs it and checks to see if it's registered).
Has anyone else come across this issue, or has any pointers as to how I could resolve this? I'm not seeing anything of much help in the log between the time messages are not being received and when they are (after changing the network state). I've gone through the GCM docs and can't see any mention of messages not being received due to a time-out (on the device itself), or any config options that might affect this.
Appreciate any assistance - I can provide source if needs be, although it hardly deviates from the demo app provided in the android-sdk.
I've noticed this as well. Although I haven't dug into the actual code, here's my understanding of why this happens.
GCM (and most push messaging services) works by keeping a long-lived socket open to Google's push notification server. The socket is kept open by sending "heartbeat" messages between the phone and server.
Occasionally, the network state may change and this socket will be broken (because the IP address of the device changes, from 3g to wifi, for example). If the message comes in before the socket is reestablished, then the device will not immediately get the message.
The reconnection only happens when the phone notices the socket is broken, which only happens when it tries to send a heartbeat message.
Again, just my basic understanding of how it works and why it happens, and I could be wrong.
There are many causes for GCM message delays. If message start to arrive after you changed network state, or switched on/off airplane mode - the most likely cause is a network that closes the connection without sending FIN/RST.
GCM maintains a long-lived connection - and reconnects if it knows the connection was broken.
A router/AP/NAT is supposed to send a FIN or RST to terminate the TCP connection - so GCM and servers will know the connection is dead.
However a number of routers and mobile operators don't do this, and then GCM needs to rely on the heartbeat, ~15 min on Wifi, more on mobile. There is a trade-off between battery life/network use and heartbeat frequency.
As per your comment in above answer and according to my experience with GCM push notifications there isn't any reason that if network(internet connection) is available you should not receive push notifications I always check internet connection availability before running the application for push notification like this try checking this if this is true you should receive push notifications
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
so, If #theelfismike thinking is true, may I use something like:
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
// here the network changed to Wifi so I can send a heartbeat to GCM to keep connection
if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
//here the network changed to mobileData so I can send a heartbeat to GCM to keep connection
}
is my solution good?
in the GCMIntentSevice class ,when you receive message from server ,onMessage method gets called so at that time you can do something like...
PowerManager pm = (PowerManager) getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(
(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP),"TAG");
wakeLock.acquire();
and you have to add
<uses-permission android:name="android.permission.WAKE_LOCK" />
permission in you manifest file..
this should do the trick...
I want to develop an application which will run in the background when the wifi is turned on. The background app will check whether the device is connected to wifi network for every 15 minutes. If it is not connected then turn off the wifi in the device. Now how do I start the application in the background when wifi is turned on?
You need to create service which runs in background and you can write logic there where it checks whether wifi is available or not after every 15 mins.
If it detect wifi then run your background operation in the same service.
you can refer this
http://stacktips.com/tutorials/android/android-service-example
Also you can detect the broadcast while system detects wifi.
public class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conMan = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() ==
ConnectivityManager.TYPE_WIFI)
Log.d("WifiReceiver", "Have Wifi Connection");
else
Log.d("WifiReceiver", "Don't have Wifi Connection");
}
};
You must use a Broadcast receiver.
The event which starts the Broadcast receiver is the change of the connectivity.
To let it starts in background you can start a service when the Broadcast Receiver reveals this change of connectivity
You need to use BroadcastReceiver to get wifi event.
Releated answer explains how to do it pretty well.
While working on a little project that integrates with GCM, I've stumbled across a bit of a strange issue.
Some times when I start watching the log to see if messages are received, messages do not appear to be coming through until I have changed the network state (I.E. originally on WiFi, if I turn off WiFi and move to Mobile Data, the messages arrive fine). After I've changed the network state, messages start to arrive perfectly fine, and the same applies once I change the network state back to what it was before (in this case, WiFi) the messages continue to be received.
The project itself includes the ability to start on boot (starts the GCMBaseIntentService on boot), which again works perfectly fine, and I'm sure the app / service is running as I've manually started up the app when this issue occurs (which also checks to see if the service is running, and if it's not it runs it and checks to see if it's registered).
Has anyone else come across this issue, or has any pointers as to how I could resolve this? I'm not seeing anything of much help in the log between the time messages are not being received and when they are (after changing the network state). I've gone through the GCM docs and can't see any mention of messages not being received due to a time-out (on the device itself), or any config options that might affect this.
Appreciate any assistance - I can provide source if needs be, although it hardly deviates from the demo app provided in the android-sdk.
I've noticed this as well. Although I haven't dug into the actual code, here's my understanding of why this happens.
GCM (and most push messaging services) works by keeping a long-lived socket open to Google's push notification server. The socket is kept open by sending "heartbeat" messages between the phone and server.
Occasionally, the network state may change and this socket will be broken (because the IP address of the device changes, from 3g to wifi, for example). If the message comes in before the socket is reestablished, then the device will not immediately get the message.
The reconnection only happens when the phone notices the socket is broken, which only happens when it tries to send a heartbeat message.
Again, just my basic understanding of how it works and why it happens, and I could be wrong.
There are many causes for GCM message delays. If message start to arrive after you changed network state, or switched on/off airplane mode - the most likely cause is a network that closes the connection without sending FIN/RST.
GCM maintains a long-lived connection - and reconnects if it knows the connection was broken.
A router/AP/NAT is supposed to send a FIN or RST to terminate the TCP connection - so GCM and servers will know the connection is dead.
However a number of routers and mobile operators don't do this, and then GCM needs to rely on the heartbeat, ~15 min on Wifi, more on mobile. There is a trade-off between battery life/network use and heartbeat frequency.
As per your comment in above answer and according to my experience with GCM push notifications there isn't any reason that if network(internet connection) is available you should not receive push notifications I always check internet connection availability before running the application for push notification like this try checking this if this is true you should receive push notifications
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
so, If #theelfismike thinking is true, may I use something like:
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
// here the network changed to Wifi so I can send a heartbeat to GCM to keep connection
if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
//here the network changed to mobileData so I can send a heartbeat to GCM to keep connection
}
is my solution good?
in the GCMIntentSevice class ,when you receive message from server ,onMessage method gets called so at that time you can do something like...
PowerManager pm = (PowerManager) getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(
(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP),"TAG");
wakeLock.acquire();
and you have to add
<uses-permission android:name="android.permission.WAKE_LOCK" />
permission in you manifest file..
this should do the trick...