Constant live update - android

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.

Related

Samsung devices, no internet connection after some time in background

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!

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());
}

Which API decides if the call is going through VoLTE or VoWiFi in Android?

I want to write an xposed module where I can redirect VoLTE calls through VoWiFi. I want to know which method decides if the call is going through VoLTE or VoWifi and I would hook that method and get the work done.
Basically using WiFi I want to give the network an illusion that the device is using mobile data and send calls through wifi
Note : I am new to android programming. Excuse me if my question looks vague.
So you want to give illusion that you are connected to WIFI even though you are on mobile data.
the way we check that is this:
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
So what you can do is, hook getType method of ConnectivityManager and in afterhook method always return ConnectivityManager.TYPE_WIFI
the way to do that is by calling
param.setResult(ConnectivityManager.TYPE_WIFI);//may be you need casting here
in afterHook.
read more about connectivity here:
https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
Edit: I hope you are familiar with afterHook and beforeHook methods and how xposed works.

Android is device connected to the internet method

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;
}

How can I check if Internet connection is turned on every n seconds?

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

Categories

Resources