How to call the service of the recipient depends on the connected Internet or not?

I know how to check the availability of the Internet or not in my action, my requirement is, in fact, I store some values ​​in the database, when the device is offline (without the Internet), when it comes to online, it must transmit the database values ​​to the server without user interactivity . how can i achieve this. I got this example How to periodically check the Internet connection in the whole application? but it only works for one action, how to make it global.

+4
source share
3 answers

Use it 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); } } 
+7
source

try this feature

 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..

0
source

U need to implement the concept of services in your application. This service will run in the background and will transfer data to the server as needed Check this link for reference http://developer.android.com/guide/topics/fundamentals/services.html

0
source

Source: https://habr.com/ru/post/1411915/


All Articles