Upload data when Wi-Fi is connected? - android

I'm making an app that is essentially a web form. I fill it out and send it to a website to be processed. But I only want it to send the data when connected to wifi.
I was thinking of putting the data into a tinyDB then running a check for wifi immediately. If connected it would submit the form and delete the db entry. I'd probably also run a check when the app is loaded and closed. It's important that I don't lose the data.
Is there a better way to do this?

if the form is small, you may not even need a db, you could just use SharedPreferences
http://developer.android.com/guide/topics/data/data-storage.html#pref , unless you have multiple rows of the user answering again and again then this might not be the best solution
here's some network check code if you need it
ConnectivityManager manager =(ConnectivityManager)activityOrContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if(info==null || !info.isConnected()){
return false;// network not active
}
not sure if you need this, you may already know how to do this or have it figured out

Have a look at my question How to respect network use settings in Android. Some of the answers demonstrate how to check the network type.

You can prompt the user to ask him if he wants to enable Wifi, and then you can enable it from code. Take a look at WifiManager class.

Related

CN1 Connectivity - Concerns when internet is unstable in Public places

I'm using Connectivity library to see the internet(Wifi or network data) is active and saving the data in Storage if there is no connectivity(Offline) and synchronize with server when connected to internet. I'm having issues in public places where the internet is consistently unstable(esp. in basements, offices, stores, Coffee shops etc., where there is internet connects in and out). When I check the Connectivity is active but by the time I started synchronizing internet goes offline (Something like this). this leads inconsistent /partial updates to the server. Also, in coffee shops and Airports where wifi gets connected but there will be "Agree Terms and Conditions" page to connect. Not all the browsers will take you to that page directly after joining the Wifi. In that case I see the wifi is active in Mobile but actually it is not activated until I accept the terms and Conditions in IE or some specific browser. Any one else having difficulty in handling these kind of issue from Mobile App?
My App - Hangs on Login screen if I'm trying to login when there is in-stable/in consistent internet.It thinks wifi is there but not.
IF I'm on a screen where I will display list, screen will show blank for infinite time. Adding timeout for server request/response or something will help such scenario.
I know I'm not handling this case in code to show some kind of error message but I need some guidance to detect these conditions suing CN1 API to handle through my app.Please advise.
Code:
public boolean isOffline() {
if (Connectivity.isConnected() && forceOffline) {
relogin();
}
return forceOffline || !Connectivity.isConnected();
}
The problem is that it's impossible to detect online/offline properly as you might be connected to a local wifi/network but it might be a bad connection that won't let you reach the server. As far as the library is concerned you are connected... But in reality you don't have a connection.
First set the timeout values in NetworkManager to lower values to improve the experience although this won't solve a situation where data starts downloading and stops in the middle.
Next you need to handle these cases one by one and provide the user with a way to toggle the offline mode. Unfortunately there is no silver bullet for network reliability. You just need to go through every path and try to detect these things.

Check Updates through API & Update DB. if an internet connection is not found, then display the data stored in the DB

Every time the application is opened, it should check for updates through the API link and update the database, if an internet connection is not found, then display the data stored in the database.
How to do this ? Should I use Loaders here ?
Based on the information you provided, I suggest you refer to this answer. It shows you how to check if the device is connected to the internet.
You can check if the device is connected, if it is - Perform async task, update the DB and then display the new information.
If the device isn't connected, skip the async task and display the information that is already stored on the device. You should also display a message to the user (i.e. via snackbar or toast) that the device is not connected and you are displaying old information.
Hope this help, good luck!
Edit: Fixed typos

Get the current wifi security used on Android

I'm developping an app which may send sensitive data and I want to be sure that i don't send them on a public or weak protected network.
That's why I'd like to get the current security used on wifi network on Android.
I found this post but I'm not sure of the accuracy of the solution.
Indeed, the allowedKeyManagement method seems to return the supported protocols, but it's not explicitely said to return the current active protocol beeing used.
Is there a sure way to get the effective protection used on the cirrent wifi network ?
Thanks
I can suggest one method
Get list of all configured network using getConfiguredNetworks API
Loop through all entries and find the current Network using WifiConfiguration.status API. The status should be CURRENT for current network
For that current network, get the allowedKeyManagement and check that it is not NONE.

Enable 3G and WiFi Simultaneously

Does anyone know if its possible (programatically) to enable both 3G and WiFi to be used simultaneously, both receiving and sending packets?
I have seen various other questions on here, but with the Tethering ability inside Android now, I was wondering if this is a possibility? This has to be on a standard/stock device, and no modifications via root to the OS.
Thanks
Adam
I don't believe it is, as soon as wifi is turned on, 3g will automatically disconnect. As far as I know there is no way round it.
The only way you can achieve this is by using a APN name with the HttpConnection. This is possible in Java ME, Please visit this answer, however i have never tried it.
You need to check for the active connection, if not active first one the switch to the next connection.
public void shutup(){
SuDroid cmd = new SuDroid();
cmd.sh.runWaitFor("svc wifi enable");
cmd.sh.runWaitFor("svc data enable");
cmd.sh.runWaitFor("svc data prefer");
}
Using SuDroido

Android - no connectivity for my App. How to debug?

I have check for internet connectivity that goes like this:
public static boolean isInternetAvailable(Context ctx)
{
NetworkInfo info = ((ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected()) return false;
if (info.isRoaming())
{
return Preferences.getIsRoamingAllowed(ctx);
}
return true;
}
It's been working good for all installations so far. Today user came in with a phone where everything worked (browser, email, etc) and it wasn't roaming. But my app due to this check was giving "No connection" error.
Phone is HTC Droid ERIS with 2.1
Anyone saw this issue? Any pointers why?
Ok I have written a test application that gets the activenetwork and all networks and lets me see what is happening, I am about to go out and test this since the anomalies I am seeing happen when switching from one network to the other (as in when you go out of wifi range and into cdma etc)
Couple of things that might help regardless first you can change info.isConnected to the following
if (info == null || !info.isConnectedOrConnecting()) return false;
This makes it a little more robust in that if you are in the middle of switch over you still let the user logon
Second thing is that you said that your app denied login if roaming because your apps allow roaming preference was set to false
return Preferences.getIsRoamingAllowed(ctx);
I think you need to follow a different pattern (just my opinion) first because If the user has disallowed roaming via their settings (phone not your app) and they are on a roaming network then the .getActiveNetwork() will return null, or not connected or not available (in which case the .getReason returns "noroaming")
Personally I would let the phone decide, but if you need to restrict it then he pattern I would follow would be
Set the default to true, but note that it's the first time your activity has started and the user has had no chance to set anything (since no pref's are set this should be easy enough to detect) Detect your network connection, if you have one then also note if they are roaming
Prompt them with an alert dialog which warns them they are currently roaming and Ask them if they want to login now or wait until later
OR
Normal Login if they are not roaming
But in either case offer them the ability to set the "roaming" option the first time instead of having them figure it out themselves.
That would address your catch 22 situation and save you some phone calls, anyway that's a design decision but I think it would work out better for you than your current pattern.
Also I think instead of just telling them there is no connection you might want to tell them why, return an enum instead of a boolean and then format dependent on that.
Finally I am going to test a bit more before my final answer because I am seeing some oddity's in the network state but want to confirm my findings before giving you the results, I need this as well so it was a good time for me to dig into this.

Categories

Resources