简体   繁体   English

在多个Android设备中同时执行方法(屏幕闪烁)

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

I am new to android programming. 我是android编程的新手。 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) ? 也许获取设备的本地时间并在“ x”秒(本地时间)开始该方法?

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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