我们可以在应用程序已最小化后调用该方法吗? [英] Can we call the method after the application has been minimized?

查看:115
本文介绍了我们可以在应用程序已最小化后调用该方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS



我们可以在应用程式最小化后调用方法吗?



秒后调用 applicationDidEnterBackground:



我使用这个代码,但 code>方法不调用

   - (void)test 
{
printf Test called!);
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self performSelector:@selector(test)withObject:nil afterDelay:5.0];
}


解决方案

在你背景之后调用方法的API(只要你的任务不需要太长时间 - 通常〜10分钟是最大允许时间)。



iOS不会让应用程序在后台运行时触发计时器,所以我发现在应用程序背景之前分派后台线程,然后使该线程进入休眠状态,与计时器的效果相同。



将以下代码放在应用程序代理的 - (void)applicationWillResignActive:(UIApplication *)application 方法中:

  //分派到后台队列
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0),^ {

// Tell您要启动后台任务的系统
UIBackgroundTaskIdentifier taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^ {
//系统清除应用程序之前清除
}];

//睡眠块5秒
[NSThread sleepForTimeInterval:5.0];

//如果应用程序是后台的(而不是非活动的)调用方法
if(application.applicationState == UIApplicationStateBackground)
[self performSelector:@selector(test) ]; //或者,你可以调用[self test]; here

//告诉系统任务已经结束。
if(taskID!= UIBackgroundTaskInvalid){
[[UIApplication sharedApplication] endBackgroundTask:taskID];
}

});


iOS

Can we call the method after the application has been minimized?

For example, 5 seconds after was called applicationDidEnterBackground:.

I use this code, but test method don't call

- (void)test
{
    printf("Test called!");
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self performSelector:@selector(test) withObject:nil afterDelay:5.0];
}

解决方案

You can use the background task APIs to call a method after you've been backgrounded (as long as your task doesn't take too long - usually ~10 mins is the max allowed time).

iOS doesn't let timers fire when the app is backgrounded, so I've found that dispatching a background thread before the app is backgrounded, then putting that thread to sleep, has the same effect as a timer.

Put the following code in your app delegate's - (void)applicationWillResignActive:(UIApplication *)application method:

// Dispatch to a background queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

    // Tell the system that you want to start a background task
    UIBackgroundTaskIdentifier taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        // Cleanup before system kills the app
    }];

    // Sleep the block for 5 seconds
    [NSThread sleepForTimeInterval:5.0];

    // Call the method if the app is backgrounded (and not just inactive)
    if (application.applicationState == UIApplicationStateBackground)
        [self performSelector:@selector(test)];  // Or, you could just call [self test]; here

    // Tell the system that the task has ended.
    if (taskID != UIBackgroundTaskInvalid) {
        [[UIApplication sharedApplication] endBackgroundTask:taskID];
    }

});

这篇关于我们可以在应用程序已最小化后调用该方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆