Wednesday, June 20, 2012

Client Server Communication in Android


Here the client is the android mobile and java app is acting as the server. The client code snippet is as follows:

Socket sock=null;
       PrintStream ps=null;
System.out.println(" Trying to connect");
                       
       try
       {
              InetAddress ip =InetAddress.getByName("localhost");
              sock=new Socket("112.207.67.208",1052);//give the ip and port no:
              PrintWriter out = new PrintWriter(new BufferedWriter(new
              OutputStreamWriter (sock.getOutputStream())), true);
               out.println(“Hellooo…”);
               sock.close();  
       }
       catch(SocketException e)
       {
              System.out.println("SocketException " + e);
       }
       catch(IOException e)
       {
              System.out.println("IOException " + e);
       }



The java server code is as follows:

public class Server
{
public static final int port = 1052;
public static void main( String args[])
{
Socket sock = null;
ObjectInputStream in;
String message="";
System.out.println(" Wait…… ");
try
{
ServerSocket sersock =  new ServerSocket(port);
System.out.println("Server Running  :"+sersock);
try
{
while(true)
{
sock = sersock.accept();
System.out.println("Client Connected  :"+ sock);
in = new ObjectInputStream(sock.getInputStream());
try
{
message = (String)in.readObject();
System.out.println(message);                   
}
]
catch(ClassNotFoundException classnot){
System.err.println(“Unknown data format");
}
}
sock.close();
}
catch(SocketException se)
{
System.out.println("Server Socket Exception  "+se.getMessage());
}
}
}

Saturday, June 2, 2012

Creating a splashscreen in android

A splashscreen can be created in android using threads or countdown timer. Here I have done the splash screen using countdown timer. We have to include the image which we want to display in the <ImageView> in the xml file.
The main.xml file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="@drawable/splash"
android:id="@+id/imageView1"
android:adjustViewBounds="true">
</ImageView>
</LinearLayout>
 
Next we want to have the java code for the splash screen. Here we want to display an image for 3 seconds and then advance to a new page. This is implemented using countdown timer and the code is as follows:


package pjt.eg.splash;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;

public class Splashscreen extends Activity
{
       public void onCreate(Bundle savedInstanceState)
       {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.main);
                      creatingSplashScreen();  
       }
       private void creatingSplashScreen()
       {
              new CountDownTimer(3000, 1000)
              {
                     public void onTick(long millisUntilFinished)
                     {
             
                     }
                     public void onFinish()
                     {
                           movetohome();
                     }
               }.start();
       }
       private void movetohome()
       {
              Intent i = new Intent(this, Homepage.class);
              startActivity(i);
       }
}