谁能解释服务setForeground方法可以? [英] Can anyone explain the Service setForeground method?

查看:1832
本文介绍了谁能解释服务setForeground方法可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道更多关于在服务 setForeground()方法。

I'd like to know more about the setForeground() method in the Service class.

任何一个可以详细介绍一下?

Can any one explain it in detail?

推荐答案

setForeground()是pcated德$ P $,我认为根本就不在新版本上工作Android的。你想在新的 startForeground()代替。

setForeground() is deprecated, and I think simply does not work on newer versions of Android. You want the newer startForeground() instead.

引用我自己我的书之一:

然而,一些服务将被错过   由用户,如果他们神秘   消失。例如,默认的音乐   播放器应用程序附带   Android使用的实际服务   音乐播放。通过这种方式,用户可以   听音乐的同时继续   使用他们的电话用于其他目的。   该服务仅停止用户时,   进去和presses的停止按钮   音乐播放器的活性。如果说   服务都被关闭   出乎意料的是,用户可能不知道   什么是错的。

However, some services will be missed by the user if they mysteriously vanish. For example, the default music player application that ships with Android uses a service for the actual music playback. That way, the user can listen to music while continuing to use their phone for other purposes. The service only stops when the user goes in and presses the stop button in the music player activity. If that service were to be shut down unexpectedly, the user might wonder what is wrong.

这样的服务可以声明   自己作为的一部分   前景。这将导致其   优先级上升,使他们失去了   可能会碰到内存不足。该   权衡的是,该服务必须   保持通知,所以用户   知道这个服务被自称   前景的一部分。 ,理想的是   该通知提供了一个简单   路径返回到一些活动,其中   用户可以停止该服务。

Services like this can declare themselves as being part of the "foreground". This will cause their priority to rise and make them less likely to be bumped out of memory. The trade-off is that the service has to maintain a Notification, so the user knows that this service is claiming part of the foreground. And, ideally, that Notification provides an easy path back to some activity where the user can stop the service.

要做到()此,在的onCreate您   服务(或任何其他地方的   服务的生命是明智的),   叫 startForeground()。这需要   通知和当地独特的   整数,就像通知()方法   在 NotificationManager 。它会导致   通知来显示和移动   服务到前台优先级。   后来,你可以调用    stopForeground()来恢复正常   优先级。

To do this, in onCreate() of your service (or wherever else in the service's life it would make sense), call startForeground(). This takes a Notification and a locally-unique integer, just like the notify() method on NotificationManager. It causes the Notification to appear and moves the service into foreground priority. Later on, you can call stopForeground() to return to normal priority.

这篇关于谁能解释服务setForeground方法可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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