BlackBerry应用开发1 - GlobalEvent

BlackBerry应用开发1 - GlobalEvent

黑莓平台提供了事件模型,用来在不同的应用程序之前通信。黑莓上的任何应用程序都可以发布或者监听全局事件,发布全局事件的时候需要指定一个事件ID,一个应用所发布的事件会被所有监听全局事件的应用程序获取,不同的应用需要通过ID判断是否对该事件进行处理。
在发送全局事件的过程中可以通过发送一些简单的数据,使用起来会更简单一些。但对于大量的数据,一般采用的方式是先将应用程序保存到特定的共享空间中,然后通过事件通知目标应用。

使用GlobalEvent可以让应用之间产生即时的互动:

  • 比如setting程序里面,用户修改了程序配置以后,发GlobalEvent出去,让background程序重新初始化。
  • background程序定时发GlobalEvent给GUI程序,检测GUI程序的状态。如果GUI程序没有对GlobalEvent做出回应,说明该程序死掉了,background程序就再次启动GUI程序。

相比共享数据(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()的实现定义了当一个全局事件发生时所发生的事情.

对于全局事件的定义:

  1. 监听全局事件的应用,最好是自启动的。
  2. 定义一个静态的ID变量,使得其它的类也可以引用到。
  3. 根据ID判断是否需要响应全局事件。
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", "");

结果
BlackBerry应用开发1 - GlobalEvent_第1张图片

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); 
                } 
            }); 

你可能感兴趣的:(BlackBerry应用开发1 - GlobalEvent)