I know how to checking if internet is available or not in the activity, my requirement is, actually i am storing some values in DB when the device in offline ( no internet), when it comes to online have to pass DB values to the server with out user interactivity. how can i achieve this. i got this example How to check the Internet Connection periodically in whole application? but it's working only for one activity , how to make it as global.
Asked
Active
Viewed 1,734 times
2 Answers
7
Use this in your receiver
<receiver android:name=".UpdateReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Your UpdateRecieiver
public class UpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isConnected = activeNetInfo != null && activeNetInfo.isConnectedOrConnecting();
if (isConnected)
Log.i("NET", "connecte" +isConnected);
else Log.i("NET", "not connecte" +isConnected);
}
}

RajaReddy PolamReddy
- 22,428
- 19
- 115
- 166

Shahzad Imam
- 2,030
- 2
- 26
- 45
-
yes..............actually i have implemented the same thing in my application.offline storage to db then whenever internet connection is found it will automatically update my db on server – Shahzad Imam May 11 '12 at 06:14
-
can you share me that ..if possible else tell me how can i achieve this. – RajaReddy PolamReddy May 11 '12 at 06:20
-
not yet , i am not getting how to call this broadcast receiver and creating service , bez. upto now i did't worked on this topics.. – RajaReddy PolamReddy May 11 '12 at 06:47
-
This broadcast recever will automatically called when new network connection will be found.First give a try...Give some print message in this recever.and run the application.then disconnect data connection from setting and again connect.Then check logcat wheter something is printed in logcat – Shahzad Imam May 11 '12 at 06:52
-
if it is successful then we willl look at service – Shahzad Imam May 11 '12 at 06:55
-
how to call broad cast receiver from activity ( from every activity ) – RajaReddy PolamReddy May 11 '12 at 07:03
0
try out this function
public static boolean isInternetAvailable(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
and in the manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
enjoy..

NullPointerException
- 3,978
- 4
- 34
- 52
-
1read my question clearly, i am not asking how to check for every activity..how to make it in broadcast. – RajaReddy PolamReddy May 11 '12 at 05:22
-
well i think there is no any such broadcast receiver associated with that... But instead you can set the timer to check periodically for the internet availability in above function and as you get value true you can do http request.. – NullPointerException May 11 '12 at 05:28
-
1i can but i want pass those values to server without user interact. – RajaReddy PolamReddy May 11 '12 at 05:31
-
yes you can write the timer code in the service isn't it?? So start the service when application starts.. and do the internet checking and http request logic in the service itself – NullPointerException May 11 '12 at 05:35
-
Raja as you question.you need a service which start on USER_PRESENT an check for internet connection after specific time. if internet connection is available then try to connect server.and be careful about battery so start your service on user present and stop it on SCREEN_OFF .i think you known about these USER_PRESNET and SCREEN_ON /OFF Receiver – ρяσѕρєя K May 11 '12 at 05:35