關於 Android ios cocos2d 中的廣播監聽,androidcocos2d

來源:互聯網
上載者:User

關於 Android ios cocos2d 中的廣播監聽,androidcocos2d


1. 關於廣播監聽

第一次使用是在Android中,broadcast。主要用在2個activity之間進行傳遞資料,發出一個廣播,對這個廣播有興趣的就去監聽它,做出相應的回應即可。主要是傳遞資料,觸發機制比較好,跟全域變數或者單例有點像,但是使用場合有區別,比如2個activity之間傳遞資料,activity這種有生命週期的弄成全域變數和單例就不合適了。


2.Android的廣播


發送廣播:


 Intent intent = new Intent("OUR_BLE_CENTRAL_MANAGER_NOTIFICAION_CHANGE_STATE");            intent.putExtra("KEY_OLD_STATE", oldState);            intent.putExtra("KEY_NEW_STATE", newState);            mContext.sendBroadcast(intent);

監聽廣播:

private void registerBleBroadcastReceiver()    {        IntentFilter intentFilter = new IntentFilter();        intentFilter.addAction("OUR_BLE_CENTRAL_MANAGER_NOTIFICAION_CHANGE_STATE");        this.mContext.registerReceiver(mBleBroadcastReceiver, intentFilter);    }    private BleBroadcastReceiver mBleBroadcastReceiver;    private class BleBroadcastReceiver extends BroadcastReceiver    {        @Override        public void onReceive(Context context, Intent intent)        {            String action = intent.getAction();            if (action.equals("OUR_BLE_CENTRAL_MANAGER_NOTIFICAION_CHANGE_STATE"))            {                            }        }    }

通過intent 來存放和提取資料

3. IOS NotificationCenter


IOS中的名字不一樣,使用起來差不多:


發送Notification

NSDictionary *dictionary = [[NSDictionary alloc]init];    [[NSNotificationCenter defaultCenter] postNotificationName:@"OUR_BLE_CENTRAL_MANAGER_NOTIFICAION_CHANGE_STATE"];

監聽:

[[NSNotificationCenter defaultCenter] addObserver:cmIos selector:@selector(bleCenterManagerNotificationChangeState:) name:@"OUR_BLE_CENTRAL_MANAGER_NOTIFICAION_CHANGE_STATE" object:nil];-(void)bleCenterManagerNotificationChangeState:(NSNotification*)value{    log("bleCenterManagerNotificationChangeState");    NSDictionary *dictionary = [value object];    NSNumber *oldState = [dictionary objectForKey:KEY_OLD_STATE];    NSNumber *newState = [dictionary objectForKey:KEY_NEW_STATE];}

使用字典來傳遞資料


4. cocos2d C++中的自訂事件


老的版本的cocos2d也是用notification命名的,新的改了。

發生通知:

CustomClass retData;Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("CbCC_BLE_CENTRAL_MANAGER_NOTIFICAION_CHANGE_STATE", &retData);


接受資料:

_eventDispatcher->addCustomEventListener("CbCC_BLE_CENTRAL_MANAGER_NOTIFICAION_CHANGE_STATE", [this](EventCustom* event){        CustomClass *userData = (CustomClass *)event->getUserData();    });


可以通過自訂類來傳遞資料。

可以方便在不同的Scene,Layer之間進行傳遞資料,觸發事件。Lua的文法有點不同,本質是一樣的。


http://www.waitingfy.com/archives/1692

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.