Samsung devices, no internet connection after some time in background - android

In my application, I need to send some data to the server couple of times a day, when a specific event occurred (e.g. Battery drops below 10%)
It works well in the foreground, but in the background, the problems arise, especially on Samsung devices.
I found that after about 8 minutes in the background when trying to make an HTTP request, it always fails due to internet connection unavailability
. (It is not a doze mode issue!)
Querying the connectivity manager does approve that there is no connection at that moment.
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnected(); \\ FALSE IN THIS CASE
I thought about using JobScheduler, but the problem is that the data sending should be no longer than 30~ seconds from the moment the event occurred.
So I tried to use the last resort solution - acquiring wake lock.
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "WakeLock");
wakeLock.acquire(10000);
It helps, but 50% of the time.
It seems like that also after waking the device for 10 seconds, there is still no internet connection.
I'm all out of ideas.
Is there another solution I didn't think of?
Thanks!

Related

How to continuously active Internet on doze mode Marshmallow and Nougat

I'm working on real-time location tracking app and updating current location to the server via using foreground service. Now i'm able to keep app active while device in deep sleep. But due to Network access is suspended on Doze mode, i can't able to update it on server.
As per google developer guide suggestion we can solve this issue by using high-priority FCM messages.
But in my case how to notify server for push notification while Network access is suspended (need to identify internet access disable either by OS or manually).
One way is register for action-
"android.os.action.DEVICE_IDLE_MODE_CHANGED"
this will broadcast once the device modes will change and then figure out device is in doze/non-doze using :
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean isDeviceIdle = pm.isDeviceIdleMode();
Try to check NetworkInfo status
public static boolean isNetBlocked(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
return networkInfo != null && networkInfo.getDetailedState().name().equals(NetworkInfo.DetailedState.BLOCKED.name());
}

Not receiving GCM notification on Android 6.0.1 [duplicate]

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...

Google Cloud Messaging - messages sometimes not received until network state changed

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...

Android: HttpClient parameters - Connection and Socket Timeout

I could't find any helpful tutorials on internet nor the documentation on developers site.
In my application i am connecting to a Web Server using HttpPost, when there is no internet connection, but wifi is on it shows a white screen and after some 10-15 secs "UnknownHostException".
I caught this Exception and made toast like
Unable to connect, check your internet connection.
and close the Activity (or the Application, since i am using finish() on the 1st Activity).
When the wifi itself is off i get an instant toast like"
You need internet connection to use this Application
but the 1st case is irritating. Taking 10-15 secs time and then showing the toast.
So i used HttpParameters and added a 5 sec ConnectionTimeout parameter.
But the application works same as before(no effect of this parameters).
How can i track if i hit ConnectionTime(5 secs over). So that i can show a Toast like
Slow internet connection
moreover why is the internet connection check not working when wifi is on but no internet
this is what i check when my application is lauched:
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (isOnline(cm, this, SignUpActivity.this)){
//continue
}
public static boolean isOnline(ConnectivityManager cm, Context c, Activity a) {
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
Toast.makeText(c, "You need internet access to run this application",
Toast.LENGTH_SHORT).show();
a.finish();
return false;
}
am i only checking whether device's wifi is on. if so, how can i check whether i have internet connection, instead of just wifi
Thank You
As for the first question, try using SocketTimeout instead.
As for the second question, the line
NetworkInfo netInfo = cm.getActiveNetworkInfo();
Will only get Wi-fi status (i.e. phone wifi antenna turned on) but not actual connectivity. The function returns immediately, so that if wifi is turned off you can toast out without checking connection further. But when wi-fi is turned on, you should go on and check your server's actual reachability, with something like
InetAddress.getByName(host).isReachable(timeOut)

Constant live update

Does anybody know of a way to have my application constantly check firstly if there is internet connectivity..and secondly if there is internet connection to call a function...It needs to check for internet coverage say every 10 minutes etc?
Use ConnectivityManager to check for network connectivity.
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo.isConnected())
{
// do something
}
To wake the device every 10 minutes, use the API provided by AlarmManager. I don't have practical experience using the AlarmManager; please refer to this tutorial, which seems fairly comprehensive
Do keep in mind that waking the device this often will have a noticeable effect on battery life.

Categories

Resources