【问题标题】:How to create background event as every minute in iOS如何在iOS中每分钟创建背景事件
【发布时间】:2015-11-26 05:12:02
【问题描述】:

我想创建一个后台服务,例如,如果用户打开应用程序并在每分钟后关闭一个操作视图,则会显示该视图也会从服务器端播放该视频。这怎么可能?

【问题讨论】:

  • 什么???不清楚你在这里问什么。很混乱。
  • 其实,我想要像报警通知一样的后台通知
  • 你能说如何在 - (void)applicationDidEnterBackground:(UIApplication *)application,..state 中播放视频吗?
  • NSDate *alarmTime=[[NSDate 日期] dateByAddingTimeInterval:5.0]; UIApplication *app=[UIApplication sharedApplication]; UILocalNotification *notifyAlarm=[[UILocalNotification alloc]init]; if(notifyAlarm){ notifyAlarm.fireDate=alarmTime; notifyAlarm.timeZone=[NSTimeZone defaultTimeZone]; notifyAlarm.repeatInterval=10; notifyAlarm.soundName=@""; notifyAlarm.alertBody=@"我是这个概念的新手"; [app scheduleLocalNotification:notifyAlarm]; }
  • 以上代码每 5 秒发送一次通知,我想替换视图就像播放视频一样

标签: ios objective-c iphone ios7 cocos2d-iphone


【解决方案1】:

您可以在 3 天内完成此操作

  1. 本地通知 - 本地通知将出现,如果应用程序终止、在后台或处于活动状态,则会出现此通知。一个横幅会到达,如果你点击它,你可以决定在你的 appdelegate 中执行什么操作 - 这需要用户交互性才能工作。
  2. 推送通知 - 同上,但通知来自服务器。
  3. NSTimer - 仅在其所在的类在内存中时起作用。

创建一个 NSTimer 对象作为实例变量,以及一个整数来计数计数器。

 @implementation myClass {
NSTimer *myTimer, int counter;
}

-viewDidLoad{
counter = 61;
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkTimer) userInfo:nil repeats:YES];
}

-(void)checkTimer{
if(counter <=1){
    counter = 61;
}
else {
    counter --;
    //do your video playing work here
 }
}

【讨论】:

  • 如果我关闭我的应用程序,是否会弹出该视频?
  • 不,视频不会弹出,但会出现通知或横幅,如果用户与该横幅交互,那么您可以决定将发生什么的操作,但此时应用程序处于活动状态,因此代码必须部署在应用程序委托中。这仅适用于推送/本地通知
  • 还有其他的视频显示方式吗?
  • 那么闹钟是怎么来的,设置闹钟一段时间后它会弹出同样的方式是在iOS中弹出视频吗?
  • 据我所知,在应用程序终止或在后台播放视频时无法播放视频
【解决方案2】:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

NSCalendar *gregCalendar12 = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDateComponents *dateComponent12 = [gregCalendar12 components:NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:[NSDate date]];


[dateComponent12 setWeekday:7];
[dateComponent12 setHour:14];
[dateComponent12 setMinute:46];

UILocalNotification *notification12 = [[UILocalNotification alloc]init];
[notification12 setAlertBody:@"u got message!"];
[notification12 setFireDate:[gregCalendar12 dateFromComponents:dateComponent12]];
notification12.repeatInterval = NSCalendarUnitMinute;
notification12.applicationIconBadgeNumber+=1;
[notification12 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification12];

【讨论】:

    猜你喜欢
    • 2011-06-18
    • 1970-01-01
    • 2022-12-26
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多