安卓:屏幕旋转,对破坏和服务connundrum和服务、屏幕、connundrum

2023-09-09 21:11:46 作者:笑是她的靈魂

我已经修改了从SDK演示蓝牙聊天的例子,能够控制一个Arduino供电的蓝牙LED矩阵。使用聊天程序,我可以通过蓝牙发送邮件到显示器。我有一个问题,虽然。我已经做了两个屏幕布局,人像和风景。这样我可以有接口占据了大部分空间,在手机上,无论方向。

I've modified the bluetooth chat example from the SDK demos to be able to control an arduino powered bluetooth LED matrix. Using the chat program, I can send messages to the display via bluetooth. I have a problem though. I've done two screen layouts, a portrait and a landscape. This way I can have the interface occupy the most space on the phone, regardless of orientation.

现在的问题是,如果手机旋转时,的OnDestroy()被调用,以重新加载新的布局(横向,纵向或)。在的OnDestroy()函数我也破坏了蓝牙连接,如果是建立:

The problem is that if the phone is rotated, OnDestroy() is called, to reload the new layout (landscape, or portrait). In the OnDestroy() routine I also destroy the bluetooth link, if it is established:

   public void onDestroy() {
        super.onDestroy();
        // Stop the Bluetooth chat services
        if (mChatService != null)
            mChatService.stop();
        if (D)
            Log.e(TAG, "--- ON DESTROY ---");
    }

阅读放在这里其他职位,我发现,您可以prevent该服务被停止,在安卓configChanges =方向的清单。这样一来,当我旋转屏幕,我的蓝牙连接到显示器不再终止,但现在屏幕不会在横向模式下重绘。

Reading other posts on here, I've found that you can prevent the service from being stopped by adding "android:configChanges="orientation"" to the Manifest. Doing this, when I rotate the screen, my bluetooth link to the display is no longer terminated, however now the screen doesn't redraw in landscape mode.

要解决这个问题,我想删除如果mchatservice ......一节,这是终止连接的,但我仍然需要在code运行时,应用程序最终退出。

To fix this, I am thinking of removing the "if mchatservice..." section, which is terminating the connection, but then I will still need the code to run when the application is ultimately exited.

有没有办法让屏幕旋转时,不终止连接重绘?如果不是这样,我想我可以随时移动的服务,code到将OnPause()事件,但是这将终止连接,如果应用程序永远失去forground焦点。

Is there a way to have the screen redraw when rotated, without terminating the connect? If not, I think I can always move the service code to the OnPause() event, however this will terminate the connection if the app ever looses forground focus.

是否还有其他方法吗?

感谢。

推荐答案

如果您添加机器人:configChanges =被破坏并重新创建的方向到你的清单,以prevent的活动,您可能希望实现方法:

If you add "android:configChanges="orientation"" into your Manifest to prevent the activity from being destroyed and re-created, you might want to implement the method:

public void onConfigurationChanged(Configuration newConfig)

此方法执行每次系统配置发生变化时,即当你旋转手机和方向改变。在此方法中,你可以重新申请一个新的布局为您的活动:

This method is executed every time the system configuration is changed, i.e. when you rotate the phone and orientation is changed. Inside this method you can re-apply a new layout for your activity:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Log.e(TAG, "ORIENTATION_LANDSCAPE");
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Log.e(TAG, "ORIENTATION_PORTRAIT");
    }
}