Android Open Source - visualekg Base Activity






From Project

Back to project page visualekg.

License

The source code is released under:

GNU General Public License

If you think the Android project visualekg listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package o6systems.kalpdroid;
/*from   w  w w.  j  a v a2  s.  com*/
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import com.multv.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;

public abstract class BaseActivity extends Activity {

  int layoutID = 0;
  
  // Every Activity has an XML Message to interpret

  DatagramSocket clientSocket;
  private byte[] baReceiveBuffer;
  private static byte[]  baSendBuffer;
  UdpHandlerTask udpHandler;
  


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(layoutID != 0){
      setContentView(layoutID);
    }else{
      System.out.println("Layout ID = 0, Unexpected Condition!");
    }
    
    initViews();
    setListeners();
    constructViewviaXML();
  
    
  
    initNetwork();
  }
  
  @Override
  protected void onResume(){
    super.onResume();
  }
  
  public abstract void initViews();
  public abstract void setListeners();
  public abstract void constructViewviaXML();
  
  public void initNetwork(){
    
    try{
      clientSocket=new DatagramSocket();
      //clientSocket.setSoTimeout(1000);
    }catch(Exception e){
      System.out.println("Port Bind Error!\r\n");
    }
    baReceiveBuffer = new byte[1024];
    baSendBuffer = new byte[1024];
    
    udpHandler = new UdpHandlerTask();
    udpHandler.execute();
    
    
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
  }
  
  
  public void sendStringUDP(String strSend, String str_addr,int port){
    try {
      sendStringUDP(strSend, InetAddress.getByName(str_addr), port);
      } catch (UnknownHostException e) {
        e.printStackTrace();
      }
  }
  
  public void sendStringUDP(String strSend, InetAddress addr, int port){
    //UdpSendTask sendTask = new UdpSendTask(clientSocket,addr,port);
    //sendTask.execute(strSend);
    
    
      byte[] baTemp = strSend.getBytes();
        System.arraycopy(baTemp,0, baSendBuffer, 0, strSend.length());
        baSendBuffer [strSend.length()] = '\0';
        DatagramPacket sendPacket = new DatagramPacket(baSendBuffer, baSendBuffer.length,addr,port);
        try{
          clientSocket.send(sendPacket);
        }catch(IOException ex){
          System.out.println("Send Packet Error");
        }
    
       
  }
  
  
  
  
  
  // Inner class for udp message handling
  
  public class UdpHandlerTask extends AsyncTask<Void, Void, Integer> {
  
    
        @Override
        protected Integer doInBackground(Void... params) {
       
      System.out.println("Listening UDP Packets\r\n");
      DatagramPacket receivePacket = new DatagramPacket(baReceiveBuffer, baReceiveBuffer.length);
            
      try{
        clientSocket.receive(receivePacket);
      }catch(Exception e){
        System.out.println("Error in Packet receive\r\n");
        return -1;
      }
      
      String receivedXMLString = new String( receivePacket.getData());
      
      int iMessageLength = receivedXMLString.indexOf(0);
      receivedXMLString = receivedXMLString.substring(0,iMessageLength);
      System.out.println("Received: " + receivedXMLString);     
 
          
           // sendStringUDP(XMLString2bSent,receivePacket.getAddress(),receivePacket.getPort());
            
            return 0;
        }

        @Override
        protected void onPostExecute(Integer result) {
        }

    }
  
  public class UdpSendTask extends AsyncTask<String, Void, Integer> {
    // Android.os.networkonMainThread Exception'ini gidermek icin yapildi.
    DatagramSocket clientSocket;
    InetAddress remoteAddress;
    int remotePort;
    
    public UdpSendTask(DatagramSocket clientSocket,InetAddress remoteAddress, int remotePort){
      //this.clientSocket = clientSocket;
      try {
        this.clientSocket = new DatagramSocket();
      } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      this.remoteAddress = remoteAddress;
      this.remotePort = remotePort;
    }

        @Override
        protected Integer doInBackground(String... params) {
          String strSend = params[0];
          byte[] baTemp = strSend.getBytes();
            System.arraycopy(baTemp,0, baSendBuffer, 0, strSend.length());
            baSendBuffer [strSend.length()] = '\0';
            
            System.out.println("Sent Message:\r\n");
            System.out.println(strSend);
            
            DatagramPacket sendPacket = new DatagramPacket(baSendBuffer, baSendBuffer.length,remoteAddress,remotePort);
            try{
              clientSocket.send(sendPacket);
            }catch(IOException ex){
              System.out.println("Send Packet Error");
            }
      
            return 0;
        }

        @Override
        protected void onPostExecute(Integer result) {
        }

    }
  

}




Java Source Code List

o6systems.kalpdroid.BaseActivity.java
o6systems.kalpdroid.CameraActivity.java
o6systems.kalpdroid.CameraPreview.java
xmlMessages.ErrorDefinitions.java
xmlMessages.MessageFunction.java
xmlMessages.XMLMessage.java