有两种办法,一种是用Helper类,提供interface
可参考之前一篇:http://blog.csdn.net/xzongyuan/article/details/39757275
另一种是利用FunctionFactory,这就不需要额外类
先在controller中implements Function接口
public class PlayingSceneController extends NodeController implements Function{
实现函数
在Android的类中,通过callFunction调用。@Override public Object apply(Object o) { if(o.toString().equals("isLong")){ Logger.log("isLong"); playSprite.runAction(MoveTo.create(0.5f, new Vec2(playSprite.getPositionX(),playSprite.getPositionY()+1000))); }else{ Logger.log("isShort"); playSprite.runAction(MoveTo.create(0.5f, new Vec2(playSprite.getPositionX(),playSprite.getPositionY()+300))); } return null; }
public class HeadSetReceiver extends BroadcastReceiver{ Timer timer = null; @Override public void onReceive(Context context, Intent intent) { String intentAction = intent.getAction() ; Log.e(TAG,"onReceive"); if(Intent.ACTION_MEDIA_BUTTON.equals(intentAction)){ //获得KeyEvent对象 boolean isLong = intent.getBooleanExtra("isLong",false); if(isLong){ FunctionFactory.callFunction("buttonClick","isLong"); }else{ FunctionFactory.callFunction("buttonClick","isShort"); } /* if( ListenerHelper.getListener()!=null){ ListenerHelper.getListener().onClick(isLong); }*/ } } //终止广播(不让别的程序收到此广播,免受干扰) // abortBroadcast(); }