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