Ver Mensaje Individual
  #5 (permalink)  
Antiguo 04/04/2013, 05:58
rastafinis
 
Fecha de Ingreso: mayo-2006
Mensajes: 86
Antigüedad: 18 años
Puntos: 0
Respuesta: detectar disponibilidad de coneccion

excelente amigo gracias, pero me sale error al llamar al boleano dentro de la activity talves hago algo mal

Código:
package sg.peru.donest;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;

public class LoginActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		if (isOnline){
			String text;
			int server_port = 12345;
			byte[] message = new byte[1500];
			DatagramPacket p = new DatagramPacket(message, message.length);
			DatagramSocket s = new DatagramSocket(server_port);
			s.receive(p);
			text = new String(message, 0, p.getLength());
			Log.d("Udp tutorial","message:" + text);
			s.close();
			
		}
	}
	public boolean isOnline() {
		Context context = getApplicationContext();
		ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectMgr != null) {
			NetworkInfo[] netInfo = connectMgr.getAllNetworkInfo();
			if (netInfo != null) {
				for (NetworkInfo net : netInfo) {
					if (net.getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		} 
		
		else {
			Log.d("NETWORK", "No network available");
		}
		return false;
	}

Última edición por rastafinis; 04/04/2013 a las 06:06