android - IOS & android 如何获取应用程序的空闲状态?

标签 android iphone ios android-activity

在我的应用程序中,我有 Activity A 和 B。在 Activity B 中,我想检查应用程序的空闲状态,如果用户没有交互或最小化应用程序几分钟意味着如何将其恢复到 Activity A。我不知道'知道如何找出应用程序的空闲时间。任何人都可以帮助我解决这个问题。

最佳答案

在 iOS 中:

1) 创建一个新文件 -> Objective-C 类 -> 输入一个名称(在我的例子中是 TIMERUIApplication)并将子类更改为 UIApplication。您可能需要在子类字段中手动输入。您现在应该拥有适当的 .h 和 .m 文件。

2) 在.h文件中

#import <Foundation/Foundation.h>

//the length of time before your application "times out". This number actually represents seconds, so we'll have to multiple it by 60 in the .m file
#define kApplicationTimeoutInMinutes 5
#define kMaxIdleTimeSeconds 60.0

//the notification your AppDelegate needs to watch for in order to know that it has indeed "timed out"
#define kApplicationDidTimeoutNotification @"AppTimeOut"

@interface TIMERUIApplication : UIApplication
{
    NSTimer     *myidleTimer;
}

-(void)resetIdleTimer;

@end

3).m文件

#import "TIMERUIApplication.h"

@implementation TIMERUIApplication

//here we are listening for any touch. If the screen receives touch, the timer is reset
-(void)sendEvent:(UIEvent *)event
{
    [super sendEvent:event];

    if (!myidleTimer)
    {
        [self resetIdleTimer];
    }

    NSSet *allTouches = [event allTouches];
    if ([allTouches count] > 0)
    {
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan)
        {
            [self resetIdleTimer];
        }

    }
}
//as labeled...reset the timer
-(void)resetIdleTimer
{
    if (myidleTimer)
    {
        [myidleTimer invalidate];
    }
    //convert the wait period into minutes rather than seconds
    int timeout = kApplicationTimeoutInMinutes * 60;
    myidleTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO];

}
//if the timer reaches the limit as defined in kApplicationTimeoutInMinutes, post this notification
-(void)idleTimerExceeded
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidTimeoutNotification object:nil];
}


@end

4) 在main.m中

#import <UIKit/UIKit.h>

#import "AppDelegate.h"
#import "TIMERUIApplication.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, NSStringFromClass([TIMERUIApplication class]), NSStringFromClass([AppDelegate class]));
    }
}

5) 在 appdelegate 中

#import "AppDelegate.h"
#import "TIMERUIApplication.h"

@implementation AppDelegate

@synthesize window = _window;

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{      
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:) name:kApplicationDidTimeoutNotification object:nil];

    return YES;
}

-(void)applicationDidTimeout:(NSNotification *) notif
{
    //revert to Activity A
}

希望这对您有所帮助....

关于android - IOS & android 如何获取应用程序的空闲状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16249395/

相关文章:

iphone - 通知用户应用程序商店中应用程序的新更新作为警报并自动在 iPhone 中安装更新版本?

ios - 单元格复选标记奇数变为蓝色,而其他单元格更改 UIColor

ios - iOS 中的 PayWay API 集成程序?

java - 在客户端应用程序中声明 REST 服务 api 端点的正确模式?

android - 什么是安卓X?

iphone - 如何更改弹出窗口的外观

ios - 使用 dispatch_once 启动定位服务

objective-c - 如何在 Objective C 中释放另一个对象拥有的对象

android - 将 Android SDK 工具从 22.0.1 更新到 22.0.4(最新版本)

android - 编辑 android VideoView 帧