Jul 30, 2014

fragment






I consist 3 fragments and 1 activity.

First fragment is image page.

Second fragment is button page.

Third  fragment is web page.

And activity has empty layout and 2 button for moving page.

This is activity.

Fragment add to empty layout.


This is image fragment.


This is button fragment.


This is web fragment.



mainActivity.java

mainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void switch_fragment(int state){

    switch (state){
    case image_state:
        ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fraglayout, image, imageFrag);
        //ft.addToBackStack(null);            
        ft.commit();
        btnLeft.setText("button");
        btnRight.setText("web");
        frag_state = 1;
        break;
    case button_state:
        ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fraglayout, button, buttonFrag);
        //ft.addToBackStack(null);            
        ft.commit();
        btnLeft.setText("image");
        btnRight.setText("web");
        frag_state = 2;
        break;
    case web_state:
        ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fraglayout, web, webFrag);
        //ft.addToBackStack(null);            
        ft.commit();
        btnLeft.setText("image");
        btnRight.setText("button");
        frag_state = 3;
        break;
    default:
        break;
    }
}
 


This is switching fragment.

Line 5~8 : changing fragment.

If you active line 7, possible to using back state.

buttonFragment.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class buttonFragment extends Fragment implements OnClickListener{
 
    Button btn;
    View view;
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.button_frag, container, false);
            init();
 
            return view;
        }
    private void init(){
        btn = (Button)view.findViewById(R.id.btn);
        btn.setOnClickListener(this);
 
    }
    @Override
        public void onClick(View v) {
            switch (v.getId()){
            case R.id.btn:
                Toast.makeText(getActivity(), "hi welcome to button fragment", Toast.LENGTH_SHORT).show();
                break;
            }
 
        }
}
 


Line 14: using view for "findViewById" .

Line 22: using getActivity() instead of context.



Jul 11, 2014

UDP-NIO for Android(Client)





I tested Android(udp client)-Docklight(udp server)



MainActivity.java

  1.     @Override
  2.     public void onClick(View v) {
  3.         switch(v.getId()){
  4.        
  5.         case R.id.connect:
  6.             String ip=ipaddr.getText().toString();
  7.             int portNum=Integer.parseInt(port.getText().toString());
  8.            
  9.             try{
  10.                 client=new UdpClient(ip,portNum);
  11.             }catch(IOException e){
  12.                 e.printStackTrace();
  13.             }
  14.             InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
  15.             imm.hideSoftInputFromWindow(ipaddr.getWindowToken()0);
  16.             client.start();
  17.             connect.setEnabled(false);
  18.             disconnect.setEnabled(true);
  19.             text.setText("Connected\n");
  20.              break;
  21.         case R.id.disconnect:
  22.            
  23.             connect.setEnabled(true);
  24.             disconnect.setEnabled(false);  
  25.             text.setText("Disconnected\n");
  26.             break;
  27.        
  28.         case R.id.sendBtn:
  29.             String message;
  30.             message=msg.getText().toString();
  31.             Message m=new Message();
  32.             m.what=MSG;
  33.             m.obj="Client : "+message;
  34.             mHandler.sendMessage(m);
  35.             sendingMsg(message);
  36.            
  37.             break;
  38.        
  39.         default:
  40.         break;
  41.         }
  42.        
  43.     }
Line 5~20 : touched connect button.
Line 10 : Creating Object(thread)
Line 16 : start thread
Line 21~26 : touched disconnect button.
Line 28~37 : touched send button.

udpclient.java (reading)

  1. public class UdpClient extends Thread {
  2.     MainActivity main;
  3.     private final String hostAddress;
  4.     private int port=5000;
  5.     public static final int BUFFER_SIZE=2048;
  6.     public final static int DEFAULT_PORT=5000;
  7.    
  8.     private Boolean loop;
  9.     private static final long TIME_OUT=1000;
  10.    
  11.     public static DatagramChannel channel=null;
  12.    
  13.     public UdpClient(String Addr,int port) throws IOException{
  14.     this.hostAddress=Addr;
  15.     this.port=port;
  16.    
  17.     MainActivity.thread=new HandlerThread("HandlerThread",Process.THREAD_PRIORITY_BACKGROUND);
  18.     MainActivity.thread.start();
  19.     Looper mLooper=MainActivity.thread.getLooper();
  20.     MainActivity.mServicehandler=new ServiceHandler(mLooper);
  21.     
  22.     channel=DatagramChannel.open();
  23.     MainActivity.selector=Selector.open();
  24.    
  25.     }
  26.    
  27.     @Override
  28.     public void run() {
  29.        
  30.        
  31.         try{
  32.             final ByteBuffer readBuffer=ByteBuffer.allocate(BUFFER_SIZE);
  33.             channel.configureBlocking(false);
  34.             channel.connect(new InetSocketAddress(hostAddress, port));
  35.             channel.register(MainActivity.selector, SelectionKey.OP_READ);
  36.            
  37.            
  38.            
  39.            
  40.             loop=true;
  41.             while(loop){
  42.                
  43.                 Log.e("chk""before");
  44.                 if(MainActivity.selector.select(TIME_OUT)==0)
  45.                     continue;
  46.                
  47.                 Set<SelectionKey> readKeys=MainActivity.selector.selectedKeys();
  48.                 Iterator<SelectionKey> iterator=readKeys.iterator();
  49.                 Log.e("chk""after");
  50.                 while(iterator.hasNext()){
  51.                    
  52.                     SelectionKey key=iterator.next();
  53.                     iterator.remove();
  54.                    
  55.                     if(key.isReadable()){
  56.                         readBuffer.clear();
  57.                         int length=channel.read(readBuffer);
  58.                         readBuffer.flip();
  59.                         byte[] receivedData=new byte[length];
  60.                         System.arraycopy(readBuffer.array()0, receivedData,0, length);
  61.                         String line=new String(receivedData);
  62.                        
  63.                         Message m2=new Message();
  64.                         m2.what=MainActivity.MSG;
  65.                         m2.obj="Server : "+line;
  66.                         if(m2.obj!=null)
  67.                         MainActivity.mHandler.sendMessage(m2);
  68.                     }
  69.                 }
  70.             }
  71.         }catch(IOException e){
  72.             e.printStackTrace();
  73.         }finally{
  74.             try{
  75.                 channel.close();
  76.             }catch(IOException e){
  77.                 e.printStackTrace();
  78.             }
  79.            
  80.         }
  81.     }
  82.     public void quit(){
  83.         loop=false;
  84.         try{
  85.             channel.close();
  86.         }catch(IOException e){
  87.            
  88.         }
  89.         MainActivity.thread.quit();
  90.         MainActivity.thread.interrupt();
  91.     }
  92. }
Line 17~20 : creating HandlerThread Object and start.
Line 22~23 : open DatagramChannel and selector.
Line 34 : DatagramChannel setting NIO.
Line 35 : Connect.
Line 36 : Selector type set to Read Operation.
Line 42~71 : Reading loop.
Line 48~49 : If selector detected receive packet, transfer element.
Line 57~62 : reading.
Line 64~68 : display message.
Line 83~92 : disconnect datagramchannel

ServiceHandler.java(send)

  1. public final class ServiceHandler extends Handler{
  2.     private final ByteBuffer writeBuffer;
  3.    
  4.    
  5.     public ServiceHandler(Looper looper){
  6.         super(looper);
  7.         writeBuffer=ByteBuffer.allocate(UdpClient.BUFFER_SIZE);
  8.     }
  9.     @Override
  10.     public void handleMessage(Message msg) {
  11.         writeBuffer.clear();
  12.         String s=(String)msg.obj;
  13.         writeBuffer.put(s.getBytes());
  14.        
  15.         writeBuffer.flip();
  16.         try{
  17.             UdpClient.channel.write(writeBuffer);
  18.         }catch(IOException e){
  19.             e.printStackTrace();
  20.         }
  21.         super.handleMessage(msg);
  22.     }
  23.    
  24.    
  25. }
Line 10~23 : sending message(from sending button of Mainactivity.java )