About broadcast monitoring in Android iOS cocos2d

Source: Internet
Author: User


1. About Radio Monitoring

The first use is in Android, broadcast. It is mainly used to transfer data between 2 activity, send out a broadcast, interested in this broadcast to listen to it, to respond accordingly. The main is to pass the data, the trigger mechanism is better, with the global variable or a bit like, but there are differences in the use of occasions, such as the transfer of data between 2 activity, the activity of the life cycle of the global variables and the single case is not appropriate.


2.Android of broadcasting


Send broadcast:


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

Listen to the broadcast:

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"))            {                            }        }    }

Storing and extracting data via intent

3. IOS Notificationcenter


The names in iOS are different and are used almost as follows:


Send notification

Nsdictionary *dictionary = [[Nsdictionary alloc]init];    [[Nsnotificationcenter Defaultcenter] postnotificationname:@ "Our_ble_central_manager_notificaion_change_state"];

Listening:

[[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];}

Using dictionaries to pass data


4. Cocos2d a custom event in C + +


The old version of Cocos2d was also named after the notification, and the new one was changed.

Notification occurs:

Customclass retdata;director::getinstance ()->geteventdispatcher ()->dispatchcustomevent ("CbCC_BLE_CENTRAL_ Manager_notificaion_change_state ", &retdata);


Accept data:

_eventdispatcher->addcustomeventlistener ("Cbcc_ble_central_manager_notificaion_change_state", [This] ( Eventcustom* event) {        Customclass *userdata = (Customclass *) Event->getuserdata ();    });


You can pass data through a custom class.

It is convenient to transfer data between different scene,layer, triggering events. Lua's syntax is a little different, essentially the same.


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

About broadcast monitoring in Android iOS cocos2d

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.