简体   繁体   中英

Execute method(screen flasher) at same time in multiple android device's

I am new to android programming. I have made a simple screen flasher using multiple colors.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myView = findViewById(R.id.my_view);
    myView.setBackgroundColor(color);// set initial colour
    new Thread(new Runnable() {
        public void run() {
            while (true) {
                try {
                    Thread.sleep(INTERVAL);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                updateColor();
                whichColor = !whichColor;
            }
        }
    }).start();
}

private void updateColor() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (whichColor)
                myView.setBackgroundColor(Color.YELLOW);
            else
                myView.setBackgroundColor(Color.BLACK);
        }
    });
}

I would like to install this on multiple device and sync the flash. What i want is to sync the flash colors on all the devices. Yellow should be displayed on all device and change to Black on all devices at the same time. Is it possible? Maybe get the local time of the device and start the method at 'x' seconds(local time) ?

Create functions for different flashings. Call your functions inside this method.

   import java.util.Calendar

String CurrentTime = Calendar.getInstance().getTime().toString().trim();
String YourTime = "Enter Your Expected Time here";
int x = 3000; //Start method time in miliseconds

if(CurrentTime.equals("YourTime")){
 try {
                    sleep(x);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

 //Run Your Colour Change function or method here. like
 //colorchange(); or myView.setBackgroundColor(Color.YELLOW);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM