0

私のアプリケーションには、アクティビティ A と B があります。アクティビティ B では、アプリケーションのアイドル状態を確認したいのですが、ユーザーが数分間アプリケーションを操作または最小化しなかった場合は、それをアクティビティ AI に戻す方法を意味します。アプリケーションのアイドル時間を調べる方法.誰かがこの問題を解決するのに役立つことを知っていますか.

4

2 に答える 2

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
}

お役に立てれば....

于 2013-04-27T07:46:47.353 に答える
0

ご質問は iOS または Android に関するものですか?

iOS の AppDelegate には、アプリケーションで呼び出されるメソッドがバックグラウンドとフォアグラウンドに入り、各瞬間のタイムスタンプを保存して時間を計算できます。

また、applicationState があります (UIApplication のドキュメントを確認してください)。このプロパティを使用すると、アプリケーションがアクティブ、バックグラウンド、または非アクティブで実行されているかどうかを知ることができます。

Sunny が答えた setIdleTimerDisabled メソッドは、画面に触れずに数秒/分滞在すると画面をオフにするタイマーをアクティブ/非アクティブにするために使用されます。懐中電灯アプリケーションや加速度計を使用するゲームで使用されるのは非常に一般的です。

アンドロイドについてはわかりません。

于 2013-04-27T07:52:40.613 に答える