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());
}
Related
I'm struggling to get to know when a user gets connected to a WiFi network with Internet access.
So far, I've tried two ways to achieve that:
using a BroadcastReceiver (action: android.net.conn.CONNECTIVITY_CHANGE), and;
registering a NetworkCallback on ConnectivityManager.
Both of them fail most of the times since their callbacks are called too soon, because they are called while the network has no Internet access.
Is there a way to achieve what I want (for APIs >= 16)?
What I will suggest is to use ConnectivityManager to monitor connectivity status by implementing the following codes
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnected();
For more information please check the android Developer guide here
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 want to create a method which tells if the device is online. As far as I understand ConnectivityManager tells only if the device is connected to a network. This doesn't mean that the device is connected to the internet. To ensure that the device is online I'm using InetAddress.getByName("google.com").isReachable(3); but I can't use it on the main thread. I can create a separate thread to check the connectivity and then use a callback function but is there another way? I don't want my app to do anything before it is connected. Do you have any solutions? Thank you!
With any networking, there isn't a guaranteed way to check whether or not you are connected to an endpoint without actually sending data. Even if the device is connected to a network, has an ip address, recently received data, e.t.c, it doesn't mean that you still have a connection.
I would suggest allowing the user to progress into your application as much as possible, queuing up the requests to your server in the background whilst a connection is established. Use the same framework to resend data if the connection is lost whilst the user is using the app. Make the connection to the server as transparent to the user as possible, unless it fails to connect after ~1 minute
try this:
public static boolean isOnline(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}
Hi guys I'm new on android and I wish to check every "some" seconds if the internet connection has been turned on from the user.
This is my situation: I've a map on a fragment and I download from my app an xml file with the position of markers, titles, addresses, and many other things. Now if the person that download my app don't have the internet turned on, I can't place any markers on the map. So a popup come out that request to activate an internet connection. But if the person activate it without refresh the main activity nothing happens.
So how can I do it? Which methods I have to use?
There's no need to schedule an update based on an Internet resource if you aren't connected to the Internet. The following snippet shows how to use the ConnectivityManager to query the active network and determine if it has Internet connectivity.
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
For Determining and Monitoring the Connectivity Status Help Link
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.