我在应用程序中使用背景音频模式来播放音乐,但是当曲目列表在曲目之间跳过时,应用程序将关闭。我跟踪此错误的问题是,当我在连接 Xcode 的调试模式下运行应用程序时,应用程序将永远保留在前台!有没有办法告诉 Xcode 让应用程序进入后台模式并在调试模式下连接 Xcode?
\n
\n如标题所述,我目前无法从 UNNotificationServiceExtension 中安排任务。我想在调用以下函数并收到推送时安排任务:
func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)
目前,只要我打电话:
\n\ntry BGTaskScheduler.shared.submit(request)
输入 catch 子句并返回以下错误:\'操作无法\xe2\x80\x99t 完成。(BGTaskSchedulerErrorDomain 错误 1。)\'
\n\n推送消息通过以下网址通过 Firebase 云消息传递发送,并在我的应用程序中完美接收: https: //fcm.googleapis.com/v1/ {项目路径}。我从 POSTMAN 中调用这个 url。我还指定了访问令牌和 json 正文。
\n\n推送消息中的 apns 标头设置为“apns-push-type”:“background”。Payload.aps 具有以下值:“apns-priority”:“5”,“content-available”:1,“mutable-content”:1。我认为这一切正常,因为我验证了这一点。
\n\n奇怪的是,我确实让后台任务在实际应用程序中工作,因此无需从 UNNotificationServiceExtension 中触发它。我尝试这样做是为了检查代码是否真的有效。
\n\n我已经完成了研究,到目前为止我已经尝试了以下操作:
\n 1.我确保 info.plist 中的“允许的后台任务调度程序标识符”填充了正确的任务标识符。我已三次检查该字符串是否在代码中也正确使用。\n
\n 2.在应用程序中启用以下功能:\'后台获取\'、\'远程通知\'、\'后台处理\' .\n
\n 3.在 info.plist 中,支持以下 UIBackgroundMode:\'audio\'、\'fetch\'、\'processing\'、\'remote-notification\' 和 \'voip\'。这些相同的 UIBackgroundMode 也被输入到 UNNotificationServiceExtension info.plist 中(我这样做只是为了确定)。\n
\n 4.我确保该任务在 AppDelegate 的 \'didFinishLaunchingWithOptions\'\n
\n 5中注册. …
后台进程不属于用户和终端,守护进程也不属于用户和终端。两者之间的主要区别是什么?如果我要编写一个服务器程序,我应该将其作为后台进程还是守护进程运行?
我的问题类似于How to run Python's subprocess and left it in back,但是那里列出的答案都不适合我。
我尝试运行一个程序,例如 Slack 或 Discord(或问题更新中列出的其他程序)。即使我的脚本完成,我也希望程序能够运行。
我需要这个才能在 Windows 上工作。
注意:仅当 Slack / Discord 从 Python 脚本启动时才会出现此问题,如果之前运行过,则不会关闭。
示例代码:(如您所见,我尝试了多种方法):
import os, subprocess
from time import sleep
from subprocess import Popen, PIPE, STDOUT
# discord_path=r"C:\Program Files\Discord\Discord.exe"
# discord_path2=r"C:\Users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Discord Inc\Discord.lnk"
# os.startfile(discord_path2)
# subprocess.run([r"C:\Users\user\AppData\Local\Discord\Update.exe", "--processStart", "Discord.exe"],shell=True)
# subprocess.Popen([r"C:\Users\user\AppData\Local\Discord\Update.exe", "--processStart", "Discord.exe"],shell=True)
# subprocess.call([r"C:\Users\user\AppData\Local\Discord\Update.exe", "--processStart", "Discord.exe"])
# subprocess.Popen([r"C:\Users\user\AppData\Local\Discord\Update.exe", "--processStart", "Discord.exe"], stdin=None, stdout=None, stderr=None, close_fds=True)
# slack_path2=r"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Slack Technologies Inc\Slack.lnk"
# os.startfile(slack_path2)
# stdin=None, stdout=None, stderr=None, …Run Code Online (Sandbox Code Playgroud) 我之前使用过该&命令使脚本在后台运行另一个脚本,如下所示:
#!/bin/bash
echo "Hello World"
script1.sh &
script2.sh &
echo "Please wait..."
Run Code Online (Sandbox Code Playgroud)
但是让我说我有另一个带有IF ELSE语句的脚本,我想设置一个ELIF语句mid flow作为后台任务,&然后继续处理剩下的脚本,因为知道ELIF的其余部分将进行运行在后台:
#!/bin/bash
if cond1; then
stuff
sleep 10 &
stuff
stuff
elif cond2; then
something else
else
echo "foo"
fi
stuff
echo "Hello World"
Run Code Online (Sandbox Code Playgroud)
我真的希望这是有道理的任何帮助将不胜感激.
我想把我的应用程序作为菜单栏应用程序,我已经做到了.而且我还想跟踪正在运行的应用程序.
NSRunningApplication方法返回所有runnng应用程序.但我想检测现在激活的唯一应用程序.(用鼠标点击或命令+标签...)我怎样才能找到它?
我在下面做了代码:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(performTimerBasedUpdate) userInfo:nil repeats:YES];
}
- (void)performTimerBasedUpdate {
nowRunning = [NSRunningApplication currentApplication];
nowRunningName = [nowRunning localizedName];
}
Run Code Online (Sandbox Code Playgroud)
但是,它返回我创建的应用程序名称(self).
我终于找到了答案:谢谢你我.我可以使用过滤器获取激活的应用程序.我不知道isActive属性!
runningApplications_ = [[NSWorkspace sharedWorkspace] runningApplications];
nowRunning = [[runningApplications_ filteredArrayUsingPredicate:isActive] objectAtIndex:0];
bundleIdentifier_ = [nowRunning bundleIdentifier];
localizedName = [nowRunning localizedName];
Run Code Online (Sandbox Code Playgroud) 我用的时候
[NSTimer scheduledTimerWithTimeInterval:intervalCountDownTimer target:self selector:@selector(updateCountDownDurationForTimer:) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
如果我滚动一个,它会暂停UITableView.只有在滚动停止后才会恢复.
我的tableView用于dispatch_async & dispatch_sync异步加载图像,我不确定这是否是主要原因.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
FXDMediaItem *mediaItem = [self mediaItemAtIndexPath:indexPath];
if (mediaItem) {
dispatch_sync(dispatch_get_main_queue(), ^{
cell.imageviewAlbumArt.image = [mediaItem albumArtImage];
});
}
});
Run Code Online (Sandbox Code Playgroud)
我一直在寻找在后台模式下运行此计时器的方法,但找不到合适的解决方案.它不一定是NSTimer,但我需要使用预定的重复过程.
我怀疑答案可能实际上是一个简单的答案,但找不到它
我正在开发一个Android应用程序,当它启动时:
1)我向我的服务器发出HTTP请求以发送一个小的JSON文件.
2)打开webView以显示URL.
当服务器正常运行时,绝对没有问题,一切顺利.
但是,如果由于任何原因服务器关闭,HTTP请求字面上会挂起,我需要等到有一个HTTP timeOut,大约30秒,直到我实际看到带有URL加载的webView.
我读到我不应该在UI线程内部建立任何网络,我应该使用另一个线程.
在BlackBerry,我已经开发了应用程序,这很简单:
new Thread(){
public void run(){
HttpConnection hc =
(HttpConnection)Connector.open("http://www.stackoverflow.com");
}
}.start();
Run Code Online (Sandbox Code Playgroud)
我只是开始一个新的线程,在里面我发出请求和所有必要的网络.这样,即使我的服务器无法访问,也会立即加载webView,而不会让用户等待并感觉到应用程序实际上已挂起.
我怎样才能在Android中以最简单的方式做到这一点?
我想在一个单独的线程中在后台运行6秒后运行任务.我用这个代码.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 6 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self getUnsyncNamesFromServer];
}
Run Code Online (Sandbox Code Playgroud)
我不确定这是在后台线程中运行的.我是否需要为此目的使用dispatch_async.对于这种情况,最好的方法是什么?
从iOS上的Xcode 10开始,以下崩溃:
[Animation] +[UIView setAnimationsEnabled:] being called from a background thread. Performing any operation from a background thread on UIView or a subclass is not supported and may result in unexpected and insidious behavior. trace=...
从后台线程启动时。
+(UIImage *)circularImage:(UIImage *)image withDiameter:(NSUInteger)diameter
{
CGRect frame = CGRectMake(0.0f, 0.0f, diameter, diameter);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
[imageView setImage:image]; <--- crashing here
...
}
Run Code Online (Sandbox Code Playgroud)
我不能在后台线程中将简单的UIImage分配给UIImageView是正常的吗?
ios ×5
xcode ×3
objective-c ×2
android ×1
bash ×1
cocoa ×1
concurrency ×1
daemon ×1
if-statement ×1
linux ×1
macos ×1
nstimer ×1
plist ×1
process ×1
python ×1
python-3.x ×1
server ×1
shell ×1
subprocess ×1
swift ×1
uiimage ×1
unnotificationserviceextension ×1
windows ×1