2013-04-27 30 views
0

在我的應用程序中,我有活動A & B.在活動B我想檢查應用程序的空閒狀態,如果用戶沒有交互或最小化應用程序幾分鐘意味着如何將其恢復到活動AI不' t知道如何找出應用程序的空閒時間。可以幫助我解決這個問題。如何在IOS和android中獲取應用程序的空閒狀態?

回答

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)在的main.m 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)

#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 
} 

希望這有助於....

0

你對iOS或Android的問題嗎?

在iOS上,在您的AppDelegate中,您的應用程序將調用的方法將在後臺和前臺輸入,您可以存儲每個時刻的時間戳並計算時間。

此外,你有applicationState(檢查UIApplication文檔),使用這個屬性你可以知道你的應用程序是否正在運行active,background或inactive。

Sunny回答的setIdleTimerDisabled方法用於激活/停用定時器,當您在屏幕上保留某些秒/分鐘時關閉屏幕。手電筒應用程序或使用加速度計的遊戲使用它很常見。

我不瞭解Android。

相關問題