使用NativeExtension向AIR app 添加Activity和BroadCastReceiver(2)
开发:
Android项目
新建一个针对NativeExtension的Android项目,实现相应的FREContext,FREExtension和FREFunction等方法,同时新建一个Activity和一个BroadCastReceiver。对于如何配置Android项目来开发NativeExtension,参考http://sswilliam.blog.163.com/blog/static/1896963832011910101142574/
Android项目目录和代码如下:
CallBackExtension
CallBackExtension是NativeExtension的入口,实现了FREExtension接口,直接返回一个CallBackContext的对象实例,没啥好说的
package net.sswilliam.ane.callback;
import com.adobe.fre.FREContext; import com.adobe.fre.FREExtension;
public class CallBackExtension implements FREExtension {
@Override public FREContext createContext(String arg0) { // TODO Auto-generated method stub return new CallBackContext(); }
@Override public void dispose() { // TODO Auto-generated method stub
}
@Override public void initialize() { // TODO Auto-generated method stub
}
}
|
CallBackContext
CallBackContext是整个扩展Java部分的上下文,扩展了FREContext。首先先定义一些常量:
常量名 |
说明 |
REGISTER_RECEIVER_FUNCTION |
注册BroadCastReceiver函数的Key |
START_ACTIVITY_FUNCTION |
开启Activity函数的Key |
DATA_RECEIVE |
返回给AS端的事件 |
在CallBackContext中主要需要实现getFunctions()方法,用来返回具体的java函数的映射:
@Override public Map<String, FREFunction> getFunctions() { // TODO Auto-generated method stub if(functionMaps == null){ functionMaps = new HashMap<String, FREFunction>(); functionMaps.put(REGISTER_RECEIVER_FUNCTION, new RegisterReceiverFunction()); functionMaps.put(START_ACTIVITY_FUNCTION, new StartActivityFunction()); } return functionMaps; } |
RegisterReceiverFunction
RegisterReceiverFunction实现了FREFunction接口。其功能是向Android系统注册一个BroadCastReceiver。这里我们直接通过registerReceiver函数动态注册BroadCastReceiver而非是在AndroidManifest.xml中静态注册,主要原因是这个BroadCastReceiver的生命周期也就是这个app的生命周期,并且需要与App的FREContext进行交互,注册为静态的不是很合适。当然在AIR开发的Android程序中,也是支持在AndroidManifest.xml中静态注册BroadCastReceiver的。我们会在后面静态的注册一个Activity,静态注册BroadCastReceiver的方法和注册Activity的方法一样,因此这里就不赘述了。就像正常Android程序开发一样,Log也适用于AIR开发的Android app,可以在Logcat中查看。
@Override public FREObject call(FREContext arg0, FREObject[] arg1) { // TODO Auto-generated method stub Log.d("YZ", "register receiver"); MyReceiver receiver = new MyReceiver(arg0); arg0.getActivity().registerReceiver(receiver, new IntentFilter(MyReceiver.MYARECEIVER_ACTION)); return null; } |
StartActivityFunction
StartActivityFunction实现了FREFunction接口。其功能是打开一个我们自定义的Activity。我们会定义一个自定义的ACTION。然后在AndroidManifest.xml将这个ACTION和这个Activity进行绑定。这样我们用startActivity方法发送一个Intent,就可以开启这个Activity
@Override public FREObject call(FREContext arg0, FREObject[] arg1) { // TODO Auto-generated method stub Intent intent = new Intent(MyActivity.MYACTIVITY_ACTION); arg0.getActivity().startActivity(intent); return null; }
|
MyActivity
MyActivity就是我们自定义的用来返回结果的Activity。首先会定义MyActivity的开启的ACTION:MYACTIVITY_ACTION = "net.sswilliam.ane.callback.myactivity".
然后重写onCreate函数构建界面,并添加事件侦听。这里我们适用java编码的方式构建界面,因为我们在新建项目时,就把gen这个目录已经剔除了。我的考虑可能是因为R中存储的都是资源的地址,在原生Android的应用中没有问题,但是AIR会重新编译和打包,所以在Android中的R里的地址可能在AIR中就变了,所以R也不能用。这只是一个猜的,具体还需要实验论证,等有时间了实验论证后再公布出来。
package net.sswilliam.ane.callback;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout;
public class MyActivity extends Activity implements OnClickListener{ //声明开启Activity的Action public static final String MYACTIVITY_ACTION = "net.sswilliam.ane.callback.myactivity";
private LinearLayout layout; private EditText inputEdit; private Button submit; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub //构建界面 super.onCreate(savedInstanceState); layout = new LinearLayout(this); inputEdit = new EditText(this); submit = new Button(this); submit.setText("return"); layout.addView(inputEdit); layout.addView(submit); this.setContentView(layout); //添加事件侦听 submit.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub //像预先注册过的BroadCastReceiver发送Intent以返回数据 Intent intent = new Intent(MyReceiver.MYARECEIVER_ACTION);
intent.putExtra("data",this.inputEdit.getText().toString()); this.sendBroadcast(intent); this.finish();
}
}
|
MyReceier
MyReceiver是我们自定义的BroadCastReceiver,会被动态的注册到系统中从而侦听相应的Intent,首先我们定义需要侦听的Intent的ACTION:MYARECEIVER_ACTION = "net.sswilliam.ane.callback.myreceiver"。同时我们会传入CallBackContext的引用。这样可以在处理Intent时向Actionscript端抛出事件。
注意FREContext中有一个dispatchStatusEventAsync方法可以从java代码向Actionscript端抛出ActionScript的事件。具体请查看NaiveExtension事件机制章节。
package net.sswilliam.ane.callback;
import com.adobe.fre.FREContext;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;
public class MyReceiver extends BroadcastReceiver { private FREContext context; public static final String MYARECEIVER_ACTION = "net.sswilliam.ane.callback.myreceiver"; public MyReceiver(FREContext context){ this.context = context; }
@Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub String data = arg1.getExtras().getString("data"); this.context.dispatchStatusEventAsync(CallBackContext.DATE_RECEIVE, data);
}
} |