黑莓平台提供了事件模型,用来在不同的应用程序之前通信。黑莓上的任何应用程序都可以发布或者监听全局事件,发布全局事件的时候需要指定一个事件ID,一个应用所发布的事件会被所有监听全局事件的应用程序获取,不同的应用需要通过ID判断是否对该事件进行处理。
在发送全局事件的过程中可以通过发送一些简单的数据,使用起来会更简单一些。但对于大量的数据,一般采用的方式是先将应用程序保存到特定的共享空间中,然后通过事件通知目标应用。
使用GlobalEvent可以让应用之间产生即时的互动:
相比共享数据(RuntimeStore / PersistantStore / 数据库 / 配置文件)的方式相比,程序简化,不需要定时轮询,节省CPU使用。
注册一个全局事件
调用Application.addGlobalEventListener(GlobalEventListener)注册监听事件.
void addGlobalEventListener(GlobalEventListener listener) Adds a global event listener to this application.
net.rim.device.api.system.GlobalEventListener 接 口 的 实 现 接 收 全 局 事 件.
GlobalEventListener.eventOccurred()的实现定义了当一个全局事件发生时所发生的事情.
对于全局事件的定义:
void eventOccurred(long guid, int data0, int data1, Object object0, Object object1) Invoked when the specified global event occurred.
发布一个全局事件
使用ApplicationManager.postGlobalEvent()作为基本机制和其他进程进行通讯.
注:你也可以使用运行时存储发送和接收进程间的消息.
为了发布一个全局事件到指定的应用程序中,调用postGlobalEvent(int,long,int,int,Object,Object)。
processID 参数指定了进程的ID发送事件。为获取一个进程ID,调用 getProcessId(ApplicationDescriptor).
guid参数为事件指定一个GUID值。数据和对象为事件指定附加的信息.
To post a global event to all applications, use one of the following forms of the postGlobalEvent() method:
abstract boolean postGlobalEvent(int processId, long guid, int data0, int data1, Object object0, Object object1) Posts a global event with additional data and attachments to the specified process. boolean postGlobalEvent(long guid) Posts a global event to all applications in the system. boolean postGlobalEvent(long guid, int data0, int data1) Posts a global event with additional data to all applications in the system. abstract boolean postGlobalEvent(long guid, int data0, int data1, Object object0, Object object1) Posts a global event with additional data and attachments to all applications in the system.
代码示例:
注册全局事件监听
import net.rim.device.api.system.ApplicationManager; import net.rim.device.api.system.GlobalEventListener; import net.rim.device.api.ui.UiApplication; import net.rim.device.api.ui.component.Dialog; import net.rim.device.api.ui.container.MainScreen; /** * This class extends the UiApplication class, providing a * graphical user interface. */ public class MyApp extends UiApplication { public static final long TGE_1 = 0xec80b2e09ac7428eL; /** * Entry point for application * @param args Command line arguments (not used) */ public static void main(String[] args) { MyApp theApp = new MyApp(); theApp.enterEventDispatcher(); } /** * Creates a new MyApp object */ public MyApp() { // Push a screen onto the UI stack for rendering. MainScreen myScreen = new MainScreen(); myScreen.setTitle("Global event test screen"); pushScreen(myScreen); this.addGlobalEventListener(new GlobalEventListener() { public void eventOccurred(long guid, int data0, int data1, Object object0, Object object1) { if(guid == TGE_1){ final String msg = "Message: " +object0.toString(); UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { ApplicationManager.getApplicationManager().requestForeground(getProcessId()); Dialog.inform(msg); } }); } } }); } }
ApplicationManager.getApplicationManager().postGlobalEvent(TGE_1, 0, 0, "Test success", "");
android应用开发中也有类似的广播机制:
第一步:定义一个BroadcastReceiver广播接收类:
第二步:注册该广播:view plain private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals(ACTION_NAME)){ Toast.makeText(Test.this, "处理action名字相对应的广播", 200); } } };
第三步:触发响应view plain public void registerBoradcastReceiver(){ IntentFilter myIntentFilter = new IntentFilter(); myIntentFilter.addAction(ACTION_NAME); //注册广播 registerReceiver(mBroadcastReceiver, myIntentFilter); }
view plain mBtnMsgEvent = new Button(this); mBtnMsgEvent.setText("发送广播"); mBtnMsgEvent.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent mIntent = new Intent(ACTION_NAME); mIntent.putExtra("yaner", "发送广播,相当于在这里传送数据"); //发送广播 sendBroadcast(mIntent); } });