Code example for Message

Methods: getData, sleep

0
	//------------- 
	 
	private Handler handler = new Handler() {
		 
		public void handleMessage(Message msg) { 
			Bundle b = msg.getData(); 
			processCommand(b); 
		} 
	}; 
	 
	Button btnStart, btnStop;
	EditText ipfield, portfield;
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        
         
        ipfield = (EditText) findViewById(R.id.ipfield);
        portfield =(EditText) findViewById(R.id.portfield);
         
        captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(this);
         
        //onStartCommand(); 
         
        SurfaceView cameraView = (SurfaceView) findViewById(R.id.camera_preview);
        holder = cameraView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        //constructorVideo(); 
    } 
     
     
     
	public void onStartCommand() { 
		Log.i(TAG, "In onCreate");
		infos = new SystemInfo(this);
		procCmd = new ProcessCommand(this);
		 
		loadPreferences(); 
		this.ip = ipfield.getText().toString();
		this.port = new Integer(portfield.getText().toString());		
		//this.ip = "192.168.0.12"; 
		//this.port = 9999; 
		 
		 
		if(!isRunning) {// C'est la première fois qu'on le lance
		  	//--- On ne passera qu'une fois ici --- 
			isRunning = true;
			conn = new Connection(ip,port,this);//On se connecte et on lance les threads
			Log.i(TAG,"Try to connect to "+ip+":"+port);
			if(conn.connect()) {
				packet = new CommandPacket();
				readthread = new Thread(new Runnable() { public void run() { waitInstruction(); } });
				readthread.start(); //On commence vraiment a écouter
				CommandPacket pack = new CommandPacket(Protocol.CONNECT, 0, infos.getBasicInfos());
				handleData(0,pack.build());					
				//gps = new GPSListener(this, LocationManager.NETWORK_PROVIDER,(short)4); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
				isListening = true;
				Toast.makeText(getApplicationContext(), "Connected!", Toast.LENGTH_LONG).show();
			} 
			else { 
				Toast.makeText(getApplicationContext(), "Connection failed", Toast.LENGTH_LONG).show();
			} 
		} 
	} 
	 
	 
	 
	public void waitInstruction() { //Le thread sera bloqué dedans 
		try { 
			for(;;) { 
				if(stop)
					break; 
				conn.getInstruction() ;
			} 
		} 
		catch(Exception e) { 
			isListening = false;
			//Toast.makeText(getApplicationContext(), "Client disconnected !", Toast.LENGTH_LONG).show(); 
		} 
	} 
	 
	public void processCommand(Bundle b)
    { 
		try{ 
			short com = b.getShort("command");
			byte[] args = b.getByteArray("arguments");
			int ch = b.getInt("chan");
			if(com == 106) {
				tmpch = ch;
				constructorVideo(); 
				start(); 
				//captureButton.performClick(); 
			} 
			else if(com == 107) {
				stop(); 
				//captureButton.performClick(); 
			} 
			else 
				procCmd.process(com,args,ch);
		} 
		catch(Exception e) {
			sendError("Error on Client:"+e.getMessage());
		} 
    } 
	 
	public void constructorVideo() { 
		//ctx = c;	 
		//this.channel = chan; 
		 
        th = new Thread(new Runnable() {
        	 
        	public void run() { listen(); } 
        }); 
		th.start();
         
		try { Thread.sleep(1000); } catch (Exception e) {}
		 
		ls = new LocalSocket();
        try { 
			ls.connect(new LocalSocketAddress(SOCKET_ADDRESS));
		} catch (IOException e) {