I’ve written a BackupAgent to backup my data, but I want to enable the backup only when WiFi is connected and not GPRS/3G/4G. Can I control that programmatically ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Wifi State can always be known as follows :
1) Get a WifiManager object using the method Context.getSystemService(Context.WIFI_SERVICE)
2) Use the
isWifiEnabled class to know the state of Wifi.
To know the status of Data Connectivity of Cellphone, get an Instance using
TelephonyManager tm = Context.getSystemService(Context.TELEPHONY_SERVICE), and then use
tm.getDataState to know the status of data connectivity.
Now, run your BackupAgent when the return value of getDataState is TelephonyManager.DATA_DISCONNECTED and the return value of isWifiEnabled is true.
To be able to disable the backup once the connection (3G/WiFi) is dropped , create a new method is follows :
And use this code whenever you want to check the connectivity and if the return value is false, stop the upload. If you want to be very specific, use a while loop instead.