Android:屏幕旋转、销毁和服务难题

2023-05-18移动开发问题
7

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

问题描述

限时送ChatGPT账号..

我已经修改了 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:屏幕旋转、销毁和服务难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

收到消息“警告:在正常情况下,_fillInQueueWithExtraSpace:.."和 MPMovieP
Got the message quot;WARNING: under normal conditions, _fillInQueueWithExtraSpace:..quot; and MPMoviePlayer rotation not work in iPad IOS 5.1(收到消息“警告:在正常情况下,_fillInQueueWithExtraSpace:..和 MPMoviePlayer 旋转在 iPad IOS 5.1 中...
2024-08-12 移动开发问题
6

如何抓取 b2Body 并在屏幕上移动它?(cocos2d,box2d,iphone)
How to grab a b2Body and move it around the screen? (cocos2d,box2d,iphone)(如何抓取 b2Body 并在屏幕上移动它?(cocos2d,box2d,iphone))...
2024-08-12 移动开发问题
12

观看 Game Center 屏幕(排行榜、成就)时,如果在 iOS7 中后台运行 cocos2d 2.1 应用程序会崩
Crash if backgrounding cocos2d 2.1 app in iOS7 while watching Game Center screens (leaderboard, achievement)(观看 Game Center 屏幕(排行榜、成就)时,如果在 iOS7 中后台运行 cocos2d 2.1 应用程序会崩溃)...
2024-08-12 移动开发问题
2

cocos2d v3 在应用程序使用期间重新定向屏幕
cocos2d v3 re-orient screen during App use(cocos2d v3 在应用程序使用期间重新定向屏幕)...
2024-08-12 移动开发问题
2

Cocos2d - 设置设备/屏幕方向
Cocos2d - Setting Device/Screen Orientation(Cocos2d - 设置设备/屏幕方向)...
2024-08-12 移动开发问题
19