您的位置:首页 > 移动开发 > Android开发

消息传递之:IOS NSNotificationCenter,Android EventBus;

2015-08-28 19:38 776 查看
文章来自:http://blog.csdn.net/intbird

IOS

IOS系统自带 NSNotificationCenter

0,上图



1,初始化程序入口

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

MainViewController* mainView =  [[MainViewController alloc] init];
mainView.title  = @"Delegate";

self.navigatarController = [[UINavigationController alloc]init];
[self.navigatarController pushViewController:mainView animated:YES];
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.rootViewController = self.navigatarController;
[self.window makeKeyAndVisible];
return YES;

return YES;
}


2,注册监听和处理方法

#import "MainViewController.h"
#import "EventViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(onEvent:)
name:@"EVENT_KEY"
object:nil];
}

-(void)onEvent:(NSNotification *)notifi{
NSString * message = notifi.name ;
message = [message stringByAppendingString:notifi.object];
_labText.text = message;

UIAlertController * alert  = [UIAlertController alertControllerWithTitle:notifi.name
message:notifi.object
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * alertCacel = [UIAlertAction actionWithTitle:@"Cacel"
style:UIAlertActionStyleCancel
handler:nil];
[alert addAction:alertCacel];
[ self presentViewController:alert animated:YES completion:nil];
}

- (IBAction)sendMessage:(id)sender {
EventViewController * eventView = [[EventViewController alloc] init];
[self.navigationController pushViewController:eventView animated:YES];
}

@end


3,调用事件方法

#import "EventViewController.h"

@interface EventViewController ()

@end

@implementation EventViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

- (IBAction)sendMessage:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"EVENT_KEY" object:@"post a message!"];
}
@end


Andorid使第三方EventBus

先看下系统自带的BroadcastReceiver

Anrdoid四大组件之一的BroadcastReceiver

动态注册广播接收器,处理 onReceiver(),

通过sendBroadcast(intent)方法发送广播;

private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(action)){
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(action);
registerReceiver(receiver, intentFilter);


再看下EventBus

官方demo说明:

https://github.com/greenrobot/EventBus/blob/master/HOWTO.md

0,上图



1,gradle dependencies

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


2,注册监听,必须注意 @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(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(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,发送事件

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(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new EventBusEvent("post a message!"));
}
});
}

}


文章来自:http://blog.csdn.net/intbird

以上demo

IOS:https://github.com/intbird/IOSNSNotificationCenter

android:https://github.com/intbird/ANDIntbirdEventBus
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: