訊息傳遞之:IOS NSNotificationCenter,Android EventBus;
阿新 • • 發佈:2019-01-31
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
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!"));
}
});
}
}