0

I have created an application.

In that app I want to send a message via internet to certain person. If network is not available then the message have to stored in local DB and when internet connection comes back the message have to automatically send without clicking any refresh or sync option (Like the Whatsapp messenger).

Is there any specific code for that process?

Please help me with this issue.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Karthik
  • 67
  • 1
  • 9
  • Listen `onDataConnectionStateChanged` method in `PhoneStateListener`. http://developer.android.com/reference/android/telephony/TelephonyManager.html#DATA_CONNECTED] then send the message – Madhukar Hebbar Oct 29 '15 at 10:19

2 Answers2

0

For achieve your requirement you need continuous checking of internet connection.

So I suggest you to use a BroadcastReceiver

Broadcast receivers are used for events when connectivity comes or loose.

You can check for that events by registering that into your project but remember you need to Unregister it whenever it is not needed at all.

For example you should try this link.

Hope this answer will solve your problem.

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
0

You can use a BroadcastReceiver for your purpose.

Use this receiver in AndroidManifest:

<receiver android:name=".NetworkUpdateReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

Your NetworkUpdateReceiver:

public class NetworkUpdateReceiver 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);
         //Perform Message resend here
         }   
         else Log.i("NET", "not connecte" +isConnected);
    }
}
Exception
  • 2,273
  • 1
  • 24
  • 42