Search code examples
androidmessaging

android instant message application


i am pretty new to android, but there is something that i really want to build and i really need some advice of how and what i should be looking into.

it is basically gonna be an instant message app for android, it is an app that only works for 2 person. person A and person B, they will have to connect to each other phone somehow ( i assume using some unique ID ?), and there will be a widget on their phone. When person A press the button, person B will receive a random message that pops up on person B's phone and also vibrate on person B's phone. and when person B press it again as an reply, it will do the same to person A's phone.

what kind of library or concept should i be looking into for this kinda app?

thanks


Solution

  • https://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/

    The idea involves Socket Programming and basically letting one phone be the “server” and the other phone be the “client”. Now, I don’t know if this is standard practice for letting two phones communicate with one another, but it worked for me in a new application that I’ve been working on, and so here it is: public class ServerActivity extends Activity {

    private TextView serverStatus;
    
    // DEFAULT IP
    public static String SERVERIP = "10.0.2.15";
    
    // DESIGNATE A PORT
    public static final int SERVERPORT = 8080;
    
    private Handler handler = new Handler();
    
    private ServerSocket serverSocket;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.server);
        serverStatus = (TextView) findViewById(R.id.server_status);
    
        SERVERIP = getLocalIpAddress();
    
        Thread fst = new Thread(new ServerThread());
        fst.start();
    }
    
    public class ServerThread implements Runnable {
    
        public void run() {
            try {
                if (SERVERIP != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            serverStatus.setText("Listening on IP: " + SERVERIP);
                        }
                    });
                    serverSocket = new ServerSocket(SERVERPORT);
                    while (true) {
                        // LISTEN FOR INCOMING CLIENTS
                        Socket client = serverSocket.accept();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                serverStatus.setText("Connected.");
                            }
                        });
    
                        try {
                            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                            String line = null;
                            while ((line = in.readLine()) != null) {
                                Log.d("ServerActivity", line);
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        // DO WHATEVER YOU WANT TO THE FRONT END
                                        // THIS IS WHERE YOU CAN BE CREATIVE
                                    }
                                });
                            }
                            break;
                        } catch (Exception e) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
                                }
                            });
                            e.printStackTrace();
                        }
                    }
                } else {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            serverStatus.setText("Couldn't detect internet connection.");
                        }
                    });
                }
            } catch (Exception e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Error");
                    }
                });
                e.printStackTrace();
            }
        }
    }
    
    // GETS THE IP ADDRESS OF YOUR PHONE'S NETWORK
    private String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
                }
            }
        } catch (SocketException ex) {
            Log.e("ServerActivity", ex.toString());
        }
        return null;
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        try {
             // MAKE SURE YOU CLOSE THE SOCKET UPON EXITING
             serverSocket.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
    }
    
    }
    

    You will have a client and a server. Server sends to client. Because both phones are sending to eachother, the client and server will be constantly switching. You can also try using bluetooth implementation:

    android bluetooth implementation basics

    There are many ways to do networking, and you should look into that. But, to vibrate the phone, like you said you wanted, just use the following code:

    import android.os.Vibrator;
     ...
     Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
     // Vibrate for 500 milliseconds
     v.vibrate(500);
    

    And you will need this permission in manifest:

    <uses-permission android:name="android.permission.VIBRATE"/>
    

    Let me know if this helped!

    :-)