Android:屏幕旋转、销毁和服务难题 [英] Android: screen rotation, on destroy and services connundrum

查看:34
本文介绍了Android:屏幕旋转、销毁和服务难题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经修改了 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 ---");
    }

阅读此处的其他帖子,我发现您可以通过在清单中添加android:configChanges="orientation""来防止服务停止.这样做,当我旋转屏幕时,我到显示器的蓝牙链接不再终止,但是现在屏幕不会在横向模式下重绘.

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.

为了解决这个问题,我正在考虑删除if mchatservice..."部分,该部分会终止连接,但是当应用程序最终退出时,我仍然需要运行代码.

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.

有没有办法在旋转时重绘屏幕而不终止连接?如果没有,我想我总是可以将服务代码移至 OnPause() 事件,但是如果应用失去前台焦点,这将终止连接.

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.

还有其他方法吗?

谢谢.

推荐答案

如果您将android:configChanges="orientation"" 添加到您的 Manifest 以防止 Activity 被销毁和重新创建,您可能需要实现方法:

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

这篇关于Android:屏幕旋转、销毁和服务难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆