MediaPlayer的在不同的线程VS通过startForeground在服务运行() [英] MediaPlayer in separate Thread vs running in Service via startForeground()

查看:270
本文介绍了MediaPlayer的在不同的线程VS通过startForeground在服务运行()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我流媒体音乐在一个单独的线程。如果我离开应用程序(的onPause()的onStop()获取调用等),音乐继续播放,但最终 - 打开其他应用程序,并在它们之间切换,并要回主屏幕之后 - 我的应用程序被杀害。没有崩溃,只是一个code> WIN死亡和logcat中过程com.myapp.android死亡。显然,这是合法的应用程序将被系统以回收资源的破坏。

So, I'm streaming music in a separate thread. If I leave the app (onPause(), onStop() getting called, etc), the music continues to play, but eventually -- after opening other apps and switching between them and going back to the home screen -- my app is killed. No crash, just a WIN DEATH and process com.myapp.android has died in logcat. Obviously, it's legit for apps to be destroyed by the system in order to reclaim resources.

我的问题是:在运行的东西在一个线程关闭主(UI)线程意味着它现在有更少的优先级,只要该系统是什么呢?含义,就是它更容易被杀死比我跑的媒体播放器在服务甚至可以使用 startForeground()做在前台运行的服务?

My question is: does running stuff in a thread off of the main (UI) thread mean that it now has less priority as far as the system is concerned? Meaning, is it more likely to be killed than if I run the media player in a Service and even use startForeground() to make the service run in the foreground?

任何想法或澄清,将大大AP preciated!

Any thoughts or clarification would be greatly appreciated!

修改

此外,在服务的文件的一部分混淆了我。在相关部分,它指出:

Also, part of the documentation on Services confuses me. In pertinent part, it states:

注意:服务运行在其主办的主线流程的   服务不会创建自己的线程,并没有在运行   单独的进程(除非另行指定)。这意味着,如果   您的服务将做任何CPU密集型工作或阻塞   操作(如MP3播放或网络),你应该创建一个   该服务中的新的线程来完成这项工作。

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work.

我一直跑了MP3播放功能,在主线程上的服务和用户界面保持响应。如果我应该把它放在一个单独的线程中的建议,在上述报价,那么将不我最终回到那里,我开始,也就是与媒体播放存在的关闭主线程,从而增加了可能性,播放被杀害,当其他应用程序打开,等等?

I've always ran MP3 playback in a service on the main thread and the UI remained responsive. If I'm supposed to put it in a separate thread as recommended in the above quotation, then won't I end up back where I started, namely with the media playback occuring off of the main thread, thereby increasing the likelihood that the playback is killed when other apps are opened, etc?

推荐答案

我想大多数你的问题的回答是的安卓上的服务文档

I think most of your question is answered in the Android documentation on Services.

从链接:

一个前台服务是一个被认为是一些服务   用户积极知道,因此不为系统的候选   杀内存不足时。前台服务,必须提供   通知状态栏,这是摆在正在进行   标题,这意味着该通知不能排除,除非   该服务被停止或去除从前景

A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

要回答你的问题,是的,运行一个单独的关闭的活动的将有较低的优先级比服务在前台运行,如果活动启动该已停止/暂停。

To answer your question, yes, running a separate Thread off of an Activity is going to have lower priority than a Service running in the foreground, if the Activity that started the Thread has been stopped/paused.

就个人而言,我会建议在这样一个服务,因为这是事完全排序服务类已为创建。

Personally, I would recommend doing this in a Service, as this is exactly the sort of thing the Service class has been created for.

修改

有趣的是,你的建议是什么,似乎相反的是我已阅读(一切都敏感,当不使用线程服务)。无论如何,它大概应该是在后台线程启动。

Interesting, what you suggest seems contrary to what I have read (everything is responsive when not using thread in Service). Regardless, it probably should be started in a background thread.

该线程(即创建一个线程)不会死,除非进程死亡,并在运行时决定所需要的资源会发生。 活动的可以被杀死时,他们没有在前台,所以他们的的可得销毁。如果你有一个服务标记为前景,这是不容易被破坏,因此,你不应该有同样的问题。

The thread (a thread that YOU create) won't die unless the process dies, and that will happen when the runtime decides that resources are needed. Activity's can be killed when they are not in the foreground, so their Thread's can be destroyed too. If you have a Service marked as foreground, it is less likely to be destroyed, and therefore, you should not have the same problem.

这篇关于MediaPlayer的在不同的线程VS通过startForeground在服务运行()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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