我正在尝试让一个 Android 程序识别 Ouya 的游戏控制器。我可以使以下代码单独工作,但我正在使用我想将其插入的框架。我尝试了一些不同的东西(在另一个类中实例化该类,等等,但到目前为止我所尝试的还没有奏效。你能帮我知道该怎么做才能使它成为自己的类并让它采取行动吗?当有人使用控制器时?谢谢!
这是我的代码:
import tv.ouya.console.api.OuyaController;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class SampleGame extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OuyaController.init(this);
}
@Override
public boolean onKeyDown(final int keyCode, KeyEvent event){
//Get the player #
int player = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());
boolean handled = false;
//Handle the input
switch(keyCode){
case OuyaController.BUTTON_O:
System.out.println("Button 'O' was pressed.");
//doSomethingWithKey();
handled = true;
break;
case OuyaController.BUTTON_U:
System.out.println("Button 'U' was pressed.");
//doSomethingWithKey();
handled = true;
break;
case OuyaController.BUTTON_Y:
System.out.println("Button 'Y' was pressed.");
//doSomethingWithKey();
handled = true;
break;
case OuyaController.BUTTON_A:
System.out.println("Button 'A' was pressed.");
//doSomethingWithKey();
handled = true;
break;
}
return handled || super.onKeyDown(keyCode, event);
}
}