#endif
后台下载,发出来给大家参考参考,貌似这样只能下10分钟
//crespo add 1101
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
//crespo add 1109 to close broadcast when enter background
if (onlookerUDP)
{
[self stopAndClearTimer];
[onlookerUDP release];
onlookerUDP = nil;
}
//crespo add 1031 for back fground download
UIDevice* device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)])
{
backgroundSupported = device.multitaskingSupported;
}
if (backgroundSupported == YES)
{
//crespo add 1108
if ([[self GetCurrntNet]isEqualToString:@"3g"])
{
NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
if ([userDefault boolForKey:@"3gBackgroudDownload"] == NO)
{
return;
}
}
__block UIBackgroundTaskIdentifier bgTask;
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
//如果系统觉得我们还是运行了太久,将执行这个程序块,并停止运行应用程序
for (int i = 0; i < [TCDownloadManager sharedManager].tasks.count; i++)
{
TCDownloadTaskVideo* t = [[TCDownloadManager sharedManager].tasks objectAtIndex:i];
if (t.status == DownloadTaskStatusDownloading)
{
//stop downloading
DLog(@"stop downloading after 10 minutes later!");
[t cancel];
}
}
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
//UIBackgroundTaskInvalid表示系统没有为我们提供额外的时候
if (bgTask == UIBackgroundTaskInvalid) {
DLog(@"Failed to start background task!");
return;
}
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
//do not use NSEnumerator,has bug
// for (TCDownloadTaskVideo* t in [TCDownloadManager sharedManager].tasks)
// {
// ...
// }
for (int i = 0; i < [TCDownloadManager sharedManager].tasks.count; i++)
{
TCDownloadTaskVideo* t = [[TCDownloadManager sharedManager].tasks objectAtIndex:i];
if (t.status == DownloadTaskStatusDownloading)
{
//keep on downloading
DLog(@"start background task for downloading!");
[t cancel];
[[TCDownloadManager sharedManager]restartTask:t];
}
}
// 任务做完以后通过下面的代码通知系统该任务结束了
//[application endBackgroundTask:bgTask];
//bgTask = UIBackgroundTaskInvalid;
});
}
}
}
ios持续震动&解决震动后电影/音乐无声音bug的方法
持续震动代码:
#import < AudioToolbox/AudioToolbox.h>
#import < UIKit/UIKit.h>
void MyAudioServicesSystemSoundCompletionProc (SystemSoundID ssID,void *clientData)
{
BOOL* iShouldKeepBuzzing = clientData;
if (*iShouldKeepBuzzing)
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
else
{
//Unregister, so we don't get called again...
AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
}
}
-(void)infinityVibrate
{
BOOL iShouldKeepBuzzing = YES;
AudioServicesAddSystemSoundCompletion (
kSystemSoundID_Vibrate,
NULL,
NULL,
MyAudioServicesSystemSoundCompletionProc,
&iShouldKeepBuzzing );
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}
这么调用就行了撒:[self infinityVibrate];
播放电影的时候震动,电影无声音,解决办法:
//add by crespo to fix a bug ,when start to play movie vibrate won't disable the movie voice
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);