IOS Android: Message Event notification Nsnotificationcenter Eventbus

Source: Internet
Author: User
Tags eventbus

Article from: Http://blog.csdn.net/intbird

Iosios system comes with Nsnotificationcenter

0,

1, initialization of the program entry

- (BOOL) Application: (uiapplication*) Application Didfinishlaunchingwithoptions: (nsdictionary*) Launchoptions {mainviewcontroller* MainView = [[Mainviewcontroller alloc] init]; MainView. Title= @"Delegate"; Self. Navigatarcontroller= [[UinavigationcontrollerAlloc]init]; [ Self. NavigatarcontrollerPushviewcontroller:mainview Animated:YES]; Self. Window= [[UIWindowAlloc]initwithframe:[[uiscreen Mainscreen]bounds]]; Self. Window. Rootviewcontroller= Self. Navigatarcontroller; [ Self. WindowMakekeyandvisible];return YES;return YES;}

2, registration monitoring and processing methods

#import "MainViewController.h" #import "EventViewController.h"  @interface mainviewcontroller ()@end @implementation mainviewcontroller - (void) Viewdidload {[SuperViewdidload]; [[NsnotificationcenterDefaultcenter]addobserver: SelfSelector@selector(onEvent:) name:@"Event_key"ObjectNil];} -(void) OnEvent: (nsnotification*) notifi{NSString* Message = Notifi. Name; message = [Message Stringbyappendingstring:notifi. Object]; _labtext. Text= message; Uialertcontroller * alert = [Uialertcontroller Alertcontrollerwithtitle:notifi. NameMessage:notifi. ObjectPreferredstyle:uialertcontrollerstylealert]; Uialertaction * Alertcacel = [Uialertaction actionwithtitle:@"Cacel"Style:uialertactionstylecancel handler:Nil];    [Alert Addaction:alertcacel]; [ SelfPresentviewcontroller:alert Animated:YESCompletion:Nil];} - (ibaction) SendMessage: (IDSender {Eventviewcontroller * eventview = [[Eventviewcontroller alloc] init]; [ Self. NavigationcontrollerPushviewcontroller:eventview Animated:YES];}@end

3, invoking the event method

#import "EventViewController.h"  @interface eventviewcontroller ()@end @implementation eventviewcontroller - (void) Viewdidload {[SuperViewdidload];additional setup after loading the view from its nib.}- (ibaction) SendMessage: (ID) Sender {[[NsnotificationcenterDefaultcenter] postnotificationname:@"Event_key"object:@"Post a message!"];}@end
Andorid third-party Eventbus first look at the system comes with the Broadcastreceiver

Broadcastreceiver of one of anrdoid four components
Dynamically registers a broadcast receiver, processes Onreceiver (),
Broadcasts are sent through the Sendbroadcast (intent) method;

   privatenew BroadcastReceiver() {          @Override          publicvoidonReceive(Context context, Intent intent) {              if(intent.getAction().equals(action)){              }          }      };    new IntentFilter();    filter.addAction(action);    registerReceiver(receiver, intentFilter);
And look at the Eventbus.

Official Demo Description:
Https://github.com/greenrobot/EventBus/blob/master/HOWTO.md
0,

1,gradle dependencies

dependencies {    compile fileTree(dir‘libs‘, include: [‘*.jar‘])    ‘com.android.support:appcompat-v7:22.2.1‘    ‘de.greenrobot:eventbus:3.0.0-beta1‘}

2, register monitoring, must note @Subscribe public void method () {}

 Public  class mainactivity extends appcompatactivity {TextView Tvtext;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Eventbus.getdefault (). Register ( This); Findviewbyid (r.id.button_send). Setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {StartActivity (NewIntent (mainactivity. This, Eventbusactivity.class));        }        });    Tvtext = (TextView) Findviewbyid (R.id.textview); }@Override    protected void OnDestroy() {Super. OnDestroy (); Eventbus.getdefault (). Unregister ( This); }@Subscribe     Public void onEvent(Eventbusevent event)        {String msg = Event.getmessage ();        Tvtext.settext (msg); Toast.maketext ( This, MSG, Toast.length_long). Show (); }}

3, Send Event

 Public  class eventbusactivity extends appcompatactivity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (r.layout.activity_event); Findviewbyid (r.id.button_send). Setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {Eventbus.getdefault (). Post (NewEventbusevent ("Post a message!"));    }        }); }}

Article from: Http://blog.csdn.net/intbird
Above demo
Ios:https://github.com/intbird/iosnsnotificationcenter
Android:https://github.com/intbird/andintbirdeventbus

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS Android: Message Event notification Nsnotificationcenter Eventbus

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.