Android 版本的 NSNotificationCenter(事件绑定)
Android version of NSNotificationCenter (event binding)
android 中是否有绑定到 activity 的生命周期事件的方法?我正在制作一个库,我想连接到 onCreate 等。iOS 版本的库是这样的:
- (void)observe
{
[self bindAppEvent:UIApplicationDidBecomeActiveNotification toMethod:@selector(applicationDidBecomeActiveHandler:)];
[self bindAppEvent:UIApplicationWillResignActiveNotification toMethod:@selector(applicationWillResignActiveHandler:)];
[self bindAppEvent:UIApplicationWillTerminateNotification toMethod:@selector(applicationWillTerminateHandler:)];
}
#pragma mark Private Methods
- (void)bindAppEvent:(NSString *)appEvent toMethod:(SEL)aSelector
{
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:aSelector
name:appEvent
object:app];
}
谢谢!
您可以使用 Application.registerActivityLifecycleCallbacks 将您自己的回调注册到 activity 生命周期事件。
要求:
- API 14 级。
- 您的库需要 Application 的引用来注册回调。
android 中是否有绑定到 activity 的生命周期事件的方法?我正在制作一个库,我想连接到 onCreate 等。iOS 版本的库是这样的:
- (void)observe
{
[self bindAppEvent:UIApplicationDidBecomeActiveNotification toMethod:@selector(applicationDidBecomeActiveHandler:)];
[self bindAppEvent:UIApplicationWillResignActiveNotification toMethod:@selector(applicationWillResignActiveHandler:)];
[self bindAppEvent:UIApplicationWillTerminateNotification toMethod:@selector(applicationWillTerminateHandler:)];
}
#pragma mark Private Methods
- (void)bindAppEvent:(NSString *)appEvent toMethod:(SEL)aSelector
{
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:aSelector
name:appEvent
object:app];
}
谢谢!
您可以使用 Application.registerActivityLifecycleCallbacks 将您自己的回调注册到 activity 生命周期事件。
要求:
- API 14 级。
- 您的库需要 Application 的引用来注册回调。