Change background color has no effect once screen

2019-04-17 05:29发布

问题:

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