IOIOActivity和AppCompactActivity

时间:2017-11-15 00:02:06

标签: java android appcompatactivity ioio

我没有Java开发和从Eclipse迁移的经验。在我需要扩展AppCompactActivityIOIOActivity的情况下,我不知道如何使用嵌套类。考虑到,我有另一个内部类Looper已经扩展了另一个类。下面的代码不运行Testing类中的内容。有人可以帮助我解决如何执行我的内部类,即测试类。

我的代码:

public class MainActivity extends AppCompatActivity  {

    private class Testing extends IOIOActivity {

        private ToggleButton button_;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button_ = (ToggleButton) findViewById(R.id.toggleButton);

        }

        class Looper extends BaseIOIOLooper {
            /** The on-board LED. */
            private DigitalOutput led_;

            @Override
            protected void setup() throws ConnectionLostException {
                showVersions(ioio_, "IOIO connected!");
                led_ = ioio_.openDigitalOutput(0, true);
                enableUi(true);
            }
 @Override
            public void loop() throws ConnectionLostException, InterruptedException {
                led_.write(!button_.isChecked());
                Thread.sleep(100);
            }

            @Override
            public void disconnected() {
                enableUi(false);
                toast("IOIO disconnected");
            }

            @Override
            public void incompatible() {
                showVersions(ioio_, "Incompatible firmware version!");
            }
        }

        @Override
        protected IOIOLooper createIOIOLooper() {
            return new Looper();
        }

        private void showVersions(IOIO ioio, String title) {
            toast(String.format("%s\n" +
                            "IOIOLib: %s\n" +
                            "Application firmware: %s\n" +
                            "Bootloader firmware: %s\n" +
                            "Hardware: %s",
                    title,
                    ioio.getImplVersion(IOIO.VersionType.IOIOLIB_VER),
                    ioio.getImplVersion(IOIO.VersionType.APP_FIRMWARE_VER),
                    ioio.getImplVersion(IOIO.VersionType.BOOTLOADER_VER),
                    ioio.getImplVersion(IOIO.VersionType.HARDWARE_VER)));
        }

        private void toast(final String message) {
            final Context context = this;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
                }
            });
        }

        private int numConnected_ = 0;

        private void enableUi(final boolean enable) {
            // This is slightly trickier than expected to support a multi-IOIO use-case.
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (enable) {
                        if (numConnected_++ == 0) {
                            button_.setEnabled(true);
                        }
                    } else {
                        if (--numConnected_ == 0) {
                            button_.setEnabled(false);
                        }
                    }
                }
            });
        }
    }
}

Thankss

1 个答案:

答案 0 :(得分:0)

我找到了答案,我希望将来与大家分享。这是为了在Android Studio中启动新的IOIOActivity。 IOIO开发人员尚未编写AppCompactActivity的官方IOIO代码。经过几天的尝试,它最终经过测试并与IOIO合作。

  1. 在包中创建一个名为AppCompactIOIOActivity的新类文件(我就是这个名字)。注意:所有学分归Ytai所有。来自App507的IOIO代码
  2. public class AppCompactIOIOActivity extends AppCompatActivity implements IOIOLooperProvider {
    private final IOIOAndroidApplicationHelper helper_ = new IOIOAndroidApplicationHelper(this, this);
    
    public AppCompactIOIOActivity() {
    }
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.helper_.create();
    }
    
    protected void onDestroy() {
        this.helper_.destroy();
        super.onDestroy();
    }
    
    protected void onStart() {
        super.onStart();
        this.helper_.start();
    }
    
    protected void onStop() {
        this.helper_.stop();
        super.onStop();
    }
    
    @SuppressLint("WrongConstant")
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if((intent.getFlags() & 268435456) != 0) {
            this.helper_.restart();
        }
    
    }
    
    protected IOIOLooper createIOIOLooper() {
        throw new RuntimeException("Client must override one of the createIOIOLooper overloads!");
    }
    
    public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
        return this.createIOIOLooper();
    }
    

    }

    1. 然后在您的MainActivity中
    2. public class MainActivity extends AppCompactIOIOActivity {
      
          private ToggleButton button_;
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              button_ = (ToggleButton) findViewById(R.id.toggleButton);
      
          }
          class Looper extends BaseIOIOLooper {
              /** The on-board LED. */
              private DigitalOutput led_;
      
              @Override
              protected void setup() throws ConnectionLostException {
                  showVersions(ioio_, "IOIO connected!");
                  led_ = ioio_.openDigitalOutput(0, true);
                  enableUi(true);
              }
      
              @Override
              public void loop() throws ConnectionLostException, InterruptedException {
                  led_.write(!button_.isChecked());
                  Thread.sleep(100);
              }
      
              @Override
              public void disconnected() {
                  enableUi(false);
                  toast("IOIO disconnected");
              }
      
              @Override
              public void incompatible() {
                  showVersions(ioio_, "Incompatible firmware version!");
              }
          }
      
          @Override
          protected IOIOLooper createIOIOLooper() {
              return new Looper();
          }
      
          private void showVersions(IOIO ioio, String title) {
              toast(String.format("%s\n" +
                              "IOIOLib: %s\n" +
                              "Application firmware: %s\n" +
                              "Bootloader firmware: %s\n" +
                              "Hardware: %s",
                      title,
                      ioio.getImplVersion(IOIO.VersionType.IOIOLIB_VER),
                      ioio.getImplVersion(IOIO.VersionType.APP_FIRMWARE_VER),
                      ioio.getImplVersion(IOIO.VersionType.BOOTLOADER_VER),
                      ioio.getImplVersion(IOIO.VersionType.HARDWARE_VER)));
          }
      
          private void toast(final String message) {
              final Context context = this;
              runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      Toast.makeText(context, message, Toast.LENGTH_LONG).show();
                  }
              });
          }
      
          private int numConnected_ = 0;
      
          private void enableUi(final boolean enable) {
              // This is slightly trickier than expected to support a multi-IOIO use-case.
              runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      if (enable) {
                          if (numConnected_++ == 0) {
                              button_.setEnabled(true);
                          }
                      } else {
                          if (--numConnected_ == 0) {
                              button_.setEnabled(false);
                          }
                      }
                  }
              });
          }
      }
      

      不要忘记从IOIO开发人员那里添加资源和依赖。祝你好运!