-1

我正在 Android 开发者网站上的蓝牙聊天应用程序的帮助下开发一个蓝牙聊天应用程序。

当我将蓝牙聊天请求发送到另一台设备(假设我们之前已配对)并且请求接收设备驻留在任何活动中时,我对现有代码进行了一些修改。

我想在当前显示屏幕上显示警报,但我只在特定活动中收到警报。所以我被卡住了。我的发送方和接收方代码如下:

if (FaceCardList.requestConnection &&
    FaceCardList.deviceSelected) {

    String authorization = "messagetype:startChat,"
                            + FaceCardList.ScreenName;
    FaceCardList.this.sendMessage(authorization);
}

在接收方:

if (readMessage.indexOf("messagetype:startChat") != -1) {

    AlertDialog.Builder builder =
        new AlertDialog.Builder(FaceCardList.this);
    builder.setMessage(
        FaceCardList.screennmname + " wants to chat with you")
        .setCancelable(false)
        .setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                    Intent serverIntent = new Intent(
                            FaceCardList.this,
                            BluetoothChat.class);
                    startActivity(serverIntent);

                    String authorization = "messagetype:initiatechat";
                    FaceCardList.this
                            .sendMessage(authorization);
                }
            })
        .setNegativeButton("No",
            new DialogInterface.OnClickListener() {

                public void onClick(
                        DialogInterface diaLog,
                        int which) {
                    // TODO Auto-generated method stub

                    String authorization = "messagetype:stopservice";
                    FaceCardList.this
                            .sendMessage(authorization);

                    FaceCardList.mChatService.stop();
                    FaceCardList.mChatService.start();
                }
            });
    alert = builder.create();
    alert.show();
4

1 回答 1

0

如果您有一个正在接收请求的中央通信活动,您可以只使用处理程序轻松地与任何其他活动进行通信。

参考这里:http://developer.android.com/reference/android/os/Handler.html

有很多教程和例子。

使用处理程序非常简单且灵活 - 它不会阻止您的活动,但允许任何类型的通信,甚至传递参数

您可以使用静态方法创建一个中央通信器类,并通过该中央点发送所有请求,以根据这一点内定义的逻辑分派到您喜欢的任何活动。

这是一个带有参数的方法的示例:

  static public void sendMessageToDialogDuringChat(
        int msgID, String msgText, boolean statusBool) {
    if (theHandlerForScreenUpdate == null)  // you need to create the receiving handler within this class beforehand
        return;
    Message msgForDialogDuringChat= theHandlerForScreenUpdate
        .obtainMessage(msgID);
    Bundle bundle = new Bundle();
    bundle.putBoolean(msgText, statusBool);
    msgForDialogDuringChat.setData(bundle);
    theHandlerForScreenUpdate
        .sendMessage(msgForDialogDuringChat);

    }

这里是一个接收消息的处理程序。确保在中央调度程序中存储了应该接收消息的适当处理程序。

  public final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (DEBUG)
        Log.i(this.getClass().getSimpleName(),
            "-> "
                + Thread.currentThread().getStackTrace()[2]
                    .getMethodName() + " msg.what="
                + msg.what);
        switch (msg.what) {
        case MESSAGE_CONNECTION_SUCCESSFULL_FLAG: {
        boolean connectionOK = msg.getData().getBoolean(
            CONNECTION_SUCCESS);

        if (connectionOK)
            theStatusTV
                .setText(getString(R.string.connection_OK));
        else {
            theStatusTV
                .setText(getString(R.string.connection_NOT_OK));


        }
        }
        break;

        case MESSAGE_ALERT_FLAG: {
        ...start your alert here...

当然,您也可以在接收类中完成所有这些 - 取决于您需要如何构建“特殊”通信消息

于 2012-04-21T10:03:07.920 回答