3

Hai my Application is to Save (to Server) the Data through NetConnection. If net is not Available i saved Locally, and then when net is available again send to the server. My problem is to check Internet Connection Frequetly.So i tried the Service function for checking the Net connection. But it called once only. How to solve my Problem. Anybody kindly help me Thanks in advance!

update

package com.android.cdtech;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.view.View;

class ReceiverName extends Activity {
    BroadcastReceiver r = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
        if (cm == null)
            return;
        if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
            saveData();
        } else {
            // Do nothing or notify user somehow
        }

    }
    // code to handle broadcase

    private void saveData() {
        final saveData dh=new saveData(this); //Class for Creating a Database
        webService calService=new  webService();
        dh.open();
        Cursor c = dh.pay();

        String text = "";
        do {
            text = text + " " + "\n" + c.getString(4);
            System.out.println(c.getCount());
            // Toast.makeText(this,"Name:" +c.getString(1)+c.getString(2)+c.getString(3)+c.getString(4)+"",Toast.LENGTH_LONG).show();
            calService.paymentReceipt("PaymentReceipt", c.getString(1), c.getString(2), c.getString(3), c.getString(4), "gf", "0");
         }
         while (c.moveToNext()); 
         dh.close();
        }
    };
}
Linus Caldwell
  • 10,908
  • 12
  • 46
  • 58
Mercy
  • 1,862
  • 9
  • 39
  • 75

2 Answers2

13

You can do it without any timers, just register receiver for listening connection changes:

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

And check if connection established:

public class ReceiverName extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
        if (cm == null)
            return;
        if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
            // Send here
        } else {
            // Do nothing or notify user somehow
        }

    }
}
Jin35
  • 8,602
  • 3
  • 32
  • 52
  • 1
    final saveData dh=new saveData(this); i Cannot pass the Context here Sir if i Extend BroadcastReceiver. – Mercy Dec 26 '11 at 08:48
  • I suppose `this` is instance of `Activity`? What for do you pass activity? Usually `Context` is enough. – Jin35 Dec 26 '11 at 09:29
  • The constructor saveData(new BroadcastReceiver(){}) is undefined,i got Error like this sir.Again i update my code sir – Mercy Dec 26 '11 at 09:49
  • saveData is the Class for Creating the Database – Mercy Dec 26 '11 at 10:49
  • Just change it's constructor to SaveData(Context) - you do not need activity there – Jin35 Dec 27 '11 at 04:27
  • i cannot create context in ReceiverName sir kindly guide me – Mercy Dec 27 '11 at 09:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6155/discussion-between-java-and-jin35) – Mercy Dec 27 '11 at 10:41
  • i need ur help how can i fetch records from Activity to BroadcastReciever – W I Z A R D Sep 06 '14 at 04:54
  • @Jin35: I put toast as if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) { Toast.makeText(context, "Connected", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "Not Connected", Toast.LENGTH_SHORT).show(); }. And when i coonect to wifi first it shows "not connected" and then "connected" Same when i disconnect it show "not connected" 2 times. I used same code as given here. – Manoj Fegde Jun 15 '16 at 12:59
0

you should write the code in TimerTask instead of Service because OnCreate() will execute only once throughout the Service Life cycle.

mTimerTask = new TimerTask() {

    @Override
    public void run() {

        ConnectivityManager conMgr = (ConnectivityManager)   getSystemService      (Context.CONNECTIVITY_SERVICE);
        if (conMgr.getActiveNetworkInfo() != null &&  conMgr.getActiveNetworkInfo().isAvailable() &&    conMgr.getActiveNetworkInfo().isConnected()) {
             Toast.makeText(this, "net Started", Toast.LENGTH_LONG).show();
             saveData();
        }

     }
};

// Check Internet connection in every 5 sec

mTimer = new Timer();
mTimer.scheduleAtFixedRate(mTimerTask,1000,5000);
Sujit
  • 10,512
  • 9
  • 40
  • 45