0

アプリケーションがバックグラウンド状態になると、バックグラウンドでの実行に使用される時間が機能しなくなります。以下はコードです。

AppDelegate.h では、

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    BOOL status;
    UIBackgroundTaskIdentifier bgTask;
}

AppDelegate.m 内

    - (void)applicationDidEnterBackground:(UIApplication *)application
{

    NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
    }];

    dispatch_async(dispatch_get_main_queue(), ^{

        if ([application backgroundTimeRemaining] > 1.0) {
            // Start background service synchronously
            [[vController getInstance] run];

        }

        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;

    });
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
    }];

    dispatch_async(dispatch_get_main_queue(), ^{

        if ([application backgroundTimeRemaining] > 1.0) {

            // Start background service synchronously
            [[vController getInstance] stopbackground];

        }

        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;

    });
}

ビュー Controller.h では、

@interface vController : UIViewController <AVAudioPlayerDelegate>
-(void) run;
-(void) stopbackground;
-(void) getMessage:(NSTimer*)theTimer;;
+(vController*) getInstance;
@property (nonatomic,retain) NSTimer * timer;
@end

ビューコントローラー.m、

@implementation vController
@synthesize timer;
vController *tvc;

- (void)viewDidLoad
{
    [super viewDidLoad];
    tvc = self;
    // Do any additional setup after loading the view, typically from a nib.
}

+ (vController*) getInstance
{
    return tvc;
}

- (void)stopbackground
{
    timer = [NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(getMessage:) userInfo:nil repeats:YES];
}

- (void)run
{
    timer = [NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(getMessage:) userInfo:nil repeats:YES];
}

- (void) getMessage:(NSTimer*) theTimer
{

    NSError *error;

    NSString *path = [[NSBundle mainBundle] pathForResource:@"alert" ofType:@"mp3"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
    if (theAudio == nil) {
        NSLog(@"%@", [error description]);
    }
    NSLog(@"Hi");
    theAudio.delegate = self;
    theAudio.numberOfLoops = 1;
    [theAudio play];
}

- (void) dealloc{
    [timer release];
    [super dealloc];
}

シミュレーター6.0で使用しています

4

2 に答える 2

0

ユースケースによっては、これが不可能な場合があります。つまり、Appleは、次のいずれかのタスクを実行していない限り、「バックグラウンド」でコードを実行することを許可していません。

  • 音楽プレーヤーアプリなど、バックグラウンドでユーザーに音声コンテンツを再生するアプリ
  • ナビゲーションアプリなど、ユーザーに自分の位置を常に通知するアプリ
  • ボイスオーバーインターネットプロトコル(VoIP)をサポートするアプリ
  • 新しいコンテンツをダウンロードして処理する必要があるニューススタンドアプリ
  • 外部アクセサリから定期的に更新を受け取るアプリ

詳細については、http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.htmlをご覧ください。

于 2013-03-09T07:04:27.383 に答える
0

インスタンスを作成することで、以下のような任意のビュー コントローラーでバックグラウンド タスクを実行できます。

//YourViewController.hファイルで次のように宣言します。

+ (YourViewController *)getInstance;
- (void)run;
-(void)stopbackground;

// YourViewController.m ファイルで Vollowing を定義します。

static  YourViewController *instance = NULL;

+(YourViewController *)getInstance {

    @synchronized(self) {
        if (instance == NULL) {
            instance = [[self alloc] init];
        }
    }
    return instance;
}


- (void)run

{
// strat Back ground task
}
-(void)stopbackground
{
// Stop Background task
}

AppDelegate.hファイル内で次を宣言します

UIBackgroundTaskIdentifier bgTask; 

AppDelegate.mファイルでは、次のメソッドを使用します。

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"Application entered background state.");

    // bgTask is instance variable
    NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
    }];

    dispatch_async(dispatch_get_main_queue(), ^{

        if ([application backgroundTimeRemaining] > 1.0) {
            // Start background service synchronously
[[YourViewController getInstance] run];

        }

        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;

    });
}


- (void)applicationWillEnterForeground:(UIApplication *)application
{

    NSLog(@"Application entered background state.");

    // bgTask is instance variable
    NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
    }];

    dispatch_async(dispatch_get_main_queue(), ^{

        if ([application backgroundTimeRemaining] > 1.0) {

 // Start background service synchronously
  [[YourViewController getInstance] stopbackground];

        }

        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;

    });


    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
于 2013-03-09T08:32:17.917 に答える