Change background color has no effect once screen has been rotated

故事扮演 提交于 2019-12-06 14:57:44

问题


all I am new to Android and now I am facing a strange problem, the problem in short is

"Receive TCP message and then change background color of GUI has no effect once screen has been rotated."

What I want to archive:

Have a APP running on Android device, connect to a PC via TCP. PC can send a string to APP, the APP will then do something base on string received. In this case a string, "change back ground color to red(with phone vibrates)/black" is send to APP via TCP, so Android user will see a blinding effect with vibration.

What is the problem:

I can archive what I want. But once the screen has been rotated, the color cannot be changed, only vibration remain.

What I had tried:

I put a button on the APP which will manually trigger color change event(with vibration). It works fine even if I rotate the screen.

Further test shows that UI operation such as change background color and animation triggered by TCP read are vanished, however ringtone and vibration remain unchanged.

I upload a video to Youtube http://youtu.be/n0gxXzzf-bo

Below are java code ,the button and TCP call same method: ChangeColor()

public void ChangeColor(){
    Thread t= new Thread(new ChangeColorTest());        
    t.start();
}
public class ChangeColorTest  implements Runnable{


    public void run() {             
            try {
                    for(int i=0;i<3;i++){

                    mBlinkHandler.sendEmptyMessageDelayed(1, 0);                    
                    Thread.sleep(300);              
                    mBlinkHandler.sendEmptyMessageDelayed(2, 0);
                    Thread.sleep(300);                  

                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }   


}


Handler mBlinkHandler = new Handler(){
        @Override
        public void handleMessage(Message msg){
             super.handleMessage(msg);
            //   mScreen = (LinearLayout)findViewById(R.id.mylinerlayout);
             if(msg.what==1){
             mScreen.setBackgroundColor(Color.RED);
             Vibratoration();
             }else if(msg.what==2){
             mScreen.setBackgroundColor(Color.BLACK);
             }
        }
    };

What I want to know?

If someone could possible figure out a solution, it would be much appreciated.

What is my platform? Server PC: Win7 64bits VS2010 C# Android platform: 4.0 Samsung S2 Development IDE:Motodev SDK API: Android 3


回答1:


Android will recreate the whole activity when you rotate the screen - so probably your variable "mScreen" won't point to the instance actually visible on the screen after rotation.

You can avoid, that android recreates the activity as explained here: http://developer.android.com/guide/topics/resources/runtime-changes.html




回答2:


One solution would be to lock the screen so it can't be rotated? (Or at least lock it during this functionality.)

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


来源:https://stackoverflow.com/questions/12058774/change-background-color-has-no-effect-once-screen-has-been-rotated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!