码迷,mamicode.com
首页 > 移动开发 > 详细

IOS Android:消息事件通知 NSNotificationCenter EventBus

时间:2015-08-28 21:39:27      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:nsnotifica   eventbus   

文章来自: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

版权声明:本文为博主原创文章,未经博主允许不得转载。

IOS Android:消息事件通知 NSNotificationCenter EventBus

标签:nsnotifica   eventbus   

原文地址:http://blog.csdn.net/intbird/article/details/48056749

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!