【问题标题】:Android: finish multiple activities on lost Bluetooth connectionAndroid:在丢失蓝牙连接时完成多项活动
【发布时间】:2012-08-15 09:20:42
【问题描述】:

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

我的问题是,当连接丢失时,我应该以某种方式完成 活动列表 活动和 交互 活动......但是如何?我听说过 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() 发送消息以便他们完成?

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    使用startActivityOnResult而不是startActivity,当你想完成活动时使用finishActivity (int requestCode)startActivityOnResult中使用的相同请求代码

    编辑:
    在 Application 中创建一个处理程序的 ArrayList,并在您的 Application 类中创建一个静态 BluetoothRemoteControlApp 字段,并将其实例化为当前的 Application 类实例。

    在每个Activity中创建一个Handler。在 Activity 的 Onresume 中,使用 BluetoothRemoteControlApp 的静态字段向 Application 注册此 Handler。

    一旦连接被删除,迭代所有的处理程序和发送消息。 在所有 Handler 中,handleMessage 使用 finish()。

    在 Onpause 中从应用程序中删除此处理程序

    【讨论】:

    • 我认为我不能这样做,因为我想从 Application 类完成活动,在扩展 ApplicationfinishActivity() 不可用。
    • 太好了,现在我也更了解 Handlers 了。
    【解决方案2】:

    您可以在建立连接后启动Activity 时设置intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET ); 之类的标志。这将允许您在重置任务时展开返回的活动堆栈。

    【讨论】:

      猜你喜欢
      • 2013-07-08
      • 1970-01-01
      • 2014-03-28
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 2011-10-15
      • 1970-01-01
      • 2016-12-13
      相关资源
      最近更新 更多