如何在IOS&amp ;;中获得应用程序的空闲状态的android?

时间:2013-04-27 07:12:02

标签: android iphone ios android-activity

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

2 个答案:

答案 0 :(得分:3)

iOS中的

1)Create a new file -> Objective-C class -> type in a name (in my case TIMERUIApplication) and change the subclass to UIApplication. You may have to manually type this in the subclass field. You should now have the appropriate .h and .m files.

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
}

希望这会有所帮助......

答案 1 :(得分:0)

您对iOS或Android有疑问吗?

在iOS上,在AppDelegate中,您的应用程序调用的方法将在后台和前台输入,您可以存储每个时刻的时间戳并计算时间。

此外,您还拥有applicationState(检查UIApplication文档),使用此属性可以了解您的应用程序是处于活动状态,后台还是非活动状态。

Sunny回答的setIdleTimerDisabled方法用于激活/停用计时器,当您在屏幕上没有任何触摸的情况下停留几秒/分钟时关闭屏幕。手电筒应用程序或使用加速度计的游戏使用它是很常见的。

我不了解Android。