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 回答