开发者

detecting internet connection in android

开发者 https://www.devze.com 2023-03-10 21:40 出处:网络
I have an app in android which is a sort of a client side of a TCP/IP connection ...this app has the use of receiving GPStdata from a GPS provider and sending it to the server side of my TCP/IP connec

I have an app in android which is a sort of a client side of a TCP/IP connection ...this app has the use of receiving GPSt data from a GPS provider and sending it to the server side of my TCP/IP connection.

Only that when there is no internet connection the GPS data I have to store it in a DB....and as soon as I have again internet connection I have to start again the client side and reconnect to my server and send the data.

Question:1.How could I detect internet connection in android?(my app is running on emulator)

2.Is possible,as soon as I detect internet connection to ask to my thread client to reconnect to the server???

Here is a scheme of my code:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.client);

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationListener = new MyLocationListener();



    cThread = new Thread(new ClientThread(syncToken));

    cThread.start();

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            locationListener);
}

public void onLocationChanged(Location loc) {
        if (loc != null) {

                    latitude = (int) (loc.getLatitude() * 1E6);

                    longitude = (int) (loc.getLongitude() * 1E6);


                    }
                }



            GeoPoint p = new GeoPoint(latitude, longitude);
            // geoPointsArray.add(p);
            db.insertData1(longitude, latitude);




        }

public class ClientThread implements Runnable {
    Object syncToken;



    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);

            socket = new Socket(serverAddr, 7001);

            Log.d(" ", "Clientul s-a conect");

        } catch (UnknownHostException e) {

            System.err.println("Don't know about host");

        } catch (IOException e) {

            System.err
                    .println("Couldn't get I/O for the connection to host");
        }

        try {

            os = new ObjectOutputStream(socket.getOutputStream());

        } catch (IOException e) {


            System.out.println(e);
        }

        while (true) {


            synchronized (syncToken) {

                try {
                    syncToken.wait();

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }
            }

            if (socket != null && os != null) {

                    try {

            //send data through the socket

                    }catch (Exception e){

                        e.printStackTrace();

                    }

                    i++;

                }

            }


    }
}

EDIT:

Here is what I did:

private NetworkStateReceiver mNetSateReceiver = null;

private class NetworkStateReceiver extends BroadcastReceiver

{
    @Override

    public void onReceive( Context context, Intent intent )

    {
        C开发者_如何学JAVAonnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo info =connectivity.getActiveNetworkInfo();;

        if (info != null && info.isConnectedOrConnecting()) {


            System.out.println(" internet connection!");


        } 
        else
            System.out.println("no internet connection");



    }
}

onCreate(){



     registerReceiver( mNetSateReceiver, new IntentFilter(
                ConnectivityManager.CONNECTIVITY_ACTION ) );

        syncToken = new Object();

        cThread = new Thread(new ClientThread(syncToken));

        cThread.start();


}

 public void onDestroy(){ 

     super.onDestroy(); 

     db.close();

        unregisterReceiver( mNetSateReceiver );
 }

I understand that everytime my state connection changes my onReceive() gets called....that means that I should start my thread in onReceive() when there is internet conection????

I'm a little bit confuse if u could clear up a little bit the thing for me...and where should I start that Intent that u are telling me?? Thx


This thread answers your question

Regarding something happening when its up. You could then raise an broadcast intent and receive the intent by an activity which will carry out the update once internet is available

I hope that helps

Edit: by broadcasting an intent i meant something like this. I'm not sure if this is the best solution someone else might provide input...

The following intent broadcast can run if internet is detected.

code to broadcast intent:

  Intent i = new Intent(DO_REFRESH);
  sendBroadcast(i);

broad to receive intent:

doRefreshBroadcastReceiver drbr;
...

onResume() {
  // register the refreshing complete broadcast receiver
  IntentFilter intentFilter = new IntentFilter(DO_REFRESH);
  drbr = new doRefreshBroadcastReceiver();
  registerReceiver(drbr,intentFilter);        
}

    public class doRefreshBroadcastReceiver extends BroadcastReceiver { 

        @Override
        public void onReceive(Context context, Intent intent) {

            // call method to run fetch code...
        }
    } 
0

精彩评论

暂无评论...
验证码 换一张
取 消