Android Service系列(二)关于Service的一些注意点

博客介绍了Service的使用方式。Service可直接启动或绑定,分别需实现onStartCommand()和onBind()方法。无论怎样启动,其他应用组件都能使用,也可将其声明为私有。此外,Service默认在主线程运行,若要执行CPU密集或阻塞操作,需自行创建新线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接上文:

段落一:Although this documentation generally discusses started and bound services separately, your service can work both ways—it can be started (to run indefinitely) and also allow binding. It's simply a matter of whether you implement a couple of callback methods: onStartCommand() to allow components to start it and onBind() to allow binding.

service 是直接启动还是bind随你便。

如果是直接启动的话就要实现onStartCommand()方法

绑定的话就要实现onBind()方法

 

段落二:

Regardless of whether your service is started, bound, or both, any application component can use the service (even from a separate application) in the same way that any component can use an activity—by starting it with an Intent. However, you can declare the service as private in the manifest file and block access from other applications. This is discussed more in the section about Declaring the service in the manifest.

不管service怎么启动,其他应用组件都能使用它(跨进程也行),----就像是其他组件都能通过Intent使用activity一样。

不过,你也能把service申明为私有,这样别人就不能用了。参考Declaring the service in the manifest.

 

段落三:

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. If your service is going to perform any CPU-intensive work or blocking operations, such as MP3 playback or networking, you should create a new thread within the service to complete that work. By using a separate thread, you can reduce the risk of Application Not Responding (ANR) errors, and the application's main thread can remain dedicated to user interaction with your activities.

简译:service默认在主线程(启动它的线程),你要work,自己开线程去。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值