1

我有一个应用程序,在主要活动中,用户选择了一个蓝牙设备并且应用程序连接到它。然后启动一个新活动,用户可以在其中选择不同的活动(与蓝牙设备交互的方式不同)。最后选择的活动被启动并且用户很高兴。

我的问题是,当连接丢失时,我应该以某种方式完成活动列表交互活动......但是如何?我听说过 Intents 在活动和广播之间传递消息,甚至是 Helpers,但没有关于如何将信息从正在运行的线程传递到多个其他活动的示例。

不同的连接线程(我从 BluetoothChat 示例中借用)位于 Application 类中,因此我可以从任何活动访问写入函数。这也是我检测到丢失连接的地方。

这是我的应用程序中的相关代码:

应用程序类:

public class BluetoothRemoteControlApp extends Application {
    public final int BT_CONNECTION_LOST = 1;

    // . . .

    private class ConnectedThread extends Thread {
        // . . .

        public void run() {
            byte[] buffer = new byte[1024];
            int bytes;

            while (true) {
                try {
                    bytes = mmInStream.read(buffer);
                    mHandler.obtainMessage(0, bytes, -1, buffer).sendToTarget();
                } catch (IOException e) {
                    e.printStackTrace();
                    // the lost connection is detected here
                    connectionLost();
                    break;
                }
            }
        }

        // . . .
    }

    private void connectionLost() {
        Log.e("BT", "Connection lost");
        // inform that connection was lost,
        // finish all activities and (re)start device select activity again
    }
}

提出不同活动(称为“行动”)的活动:

public class ActionListActivity extends ListActivity {
    ArrayList<Action> activityList = new ArrayList<Action>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // list of available activities, arguments: title, description and class name
        activityList.add(new Action("Accelerometer Control", "Control your robot by tilting the phone", "AccelerometerControl"));
        // . . .

        setListAdapter(new ActionListBaseAdapter(this, activityList));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        String activity = activityList.get(position).getClassName();

        try {
            Class<?> activityClass = Class.forName("com.bluetooth.activities." + activity);
            Intent intent = new Intent(ActionListActivity.this, activityClass);
            startActivity(intent);
        }
        catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // add a method here to finish the activity when connection is lost
}

假设我启动了我的 AccelerometerControl 活动,要输入什么connectionLost()来发送消息ActionListActivity()AccelerometerControl()以便他们完成?

4

2 回答 2

1

使用startActivityOnResult而不是 startActivity 并且当您想要完成活动时,使用具有相同请求代码的finishActivity (int requestCode)startActivityOnResult

Edit:
Create an ArrayList of Handlers in Application and also a static BluetoothRemoteControlApp field in your Application class and instantiate this to the present Application class instance.

In each Activity create a Handler. In Activity's Onresume register this Handler with Application using static field of BluetoothRemoteControlApp.

As soon as Connection is dropped, Iterate over all the handlers and sendmessage. In all Handler's , handleMessage use finish().

In Onpause remove this handler from Application

于 2012-08-15T09:24:15.710 回答
0

You can set flags like intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET ); when you launch your Activity after making a connection. This will allow you to unroll the back stack of Activities when you reset the task.

于 2012-08-15T09:36:59.157 回答