在设备上安装时如何将应用程序发送到后台? [英] How send application to background when install on device?

查看:95
本文介绍了在设备上安装时如何将应用程序发送到后台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,其中需要一些功能,例如在设备上运行我的应用程序时,它会立即关闭而不显示任何屏幕.但是应用程序在后台运行.当用户单击应用程序图标时,它将不会显示任何屏幕,但会在后台运行.间隔2分钟后,它将显示一条警报消息.怎么办?

I am making an application in which i want features like as when i run my app on device then it will closed immediately without showing any screen.But Application works in background. When user click on icon of application then it will not show any screen but work in background. After 2 minutes gap it will show a alert message. How do that?

我为此使用了以下代码:-

I have used code for this given below:-

-(void)applicationDidFinishLaunching:(UIApplication *)application{
[application cancelAllLocalNotifications];
[self applicationWillTerminate:application];}-(void)applicationWillTerminate:(UIApplication *)application{
/*
 Called when the application is about to terminate.
 Save data if appropriate.
 See also applicationDidEnterBackground:.
 */

UILocalNotification* ln = [[UILocalNotification alloc] init];
ln.fireDate =[NSDate dateWithTimeIntervalSinceNow:30];
ln.alertBody = [NSString stringWithFormat:@"Now app is working in Background."];         
ln.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
ln.hasAction=NO;
[ln release];
exit(0);}

但是,这不符合我的要求.那么这段代码中的错误是什么?怎么办?

But this is not working as i want. So what is bug in this code? How do that?

预先感谢...

推荐答案

您不能通过手动调用 [self applicationWillTerminate:application]; 来放弃应用程序.这是一个委托方法,将在您的应用程序即将终止时被调用,而不是一个终止应用程序的方法.

You can't put your app away by manually calling [self applicationWillTerminate:application];. It's a delegate method that gets called when your application is about to be terminated, not a method to terminate the app.

您可以尝试在 didFinishLaunchingWithOptions:中安排本地通知,然后再调用 exit(0); .可能会暂时显示某种标牌屏幕(或黑屏).

You could try to schedule a local notification in didFinishLaunchingWithOptions: and call exit(0); afterwards. Some kind of splah screen (or black screen) will probably be shown for a moment.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

  [application cancelAllLocalNotifications];
  UILocalNotification* ln = [[UILocalNotification alloc] init];
  ln.fireDate =[NSDate dateWithTimeIntervalSinceNow:30];
  ln.alertBody = [NSString stringWithFormat:@"Now app is working in Background."];         
  ln.soundName = UILocalNotificationDefaultSoundName;
  [[UIApplication sharedApplication] scheduleLocalNotification:ln];
  ln.hasAction=NO;
  [ln release];
  exit(0);    //this line kills the app (and gets your app rejected)
  return NO;  //this line is just to make compiler happy
}

请注意,这绝对不会被App Store批准.

Please note that this will most definetly not be approved for App Store.

这篇关于在设备上安装时如何将应用程序发送到后台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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