android 即时消息应用

android instant message application

我是 android 的新手,但我真的很想构建一些东西,我真的需要一些关于我应该如何研究以及应该研究什么的建议。

它基本上是 android 的即时消息应用程序,它是一个仅适用于 2 人的应用程序。人 A 和人 B,他们必须以某种方式相互连接 phone(我假设使用一些唯一的 ID?),并且他们的 phone 上会有一个小部件。当 A 按下按钮时,B 将收到一条随机消息,该消息会在 B 的 phone 上弹出,并在 B 的 phone 上振动。当 B 再次按下它作为回复时,它会对 A 的 phone.

做同样的事情

我应该为这种应用寻找什么样的库或概念?

感谢

您可以实施移动云来做到这一点。例如:Google 云消息、解析、... 这些取代了网络服务器来接收消息,处理它们并发送给其他人。 我推荐使用 Parse: https://parse.com/ (使用推送通知功能)。它还支持地理点,存储文件,发送消息,...

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();
     }
}

}

您将拥有一个客户端和一个服务器。服务器发送给客户端。因为两个 phone 都在互相发送,所以客户端和服务器会不断切换。您也可以尝试使用蓝牙实现:

android bluetooth implementation basics

建立网络的方法有很多,您应该研究一下。但是,要振动 phone,就像您说的那样,只需使用以下代码:

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

并且您需要在清单中获得此权限:

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

如果有帮助请告诉我!

:-)