android传值到服务,Android 开启Service两种方式及Activity与Service间进行消息传递

Service

Android开发者文档里这样定义:

A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different app, or it might fetch data over the network without blocking user interaction with an activity.Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.

Service是Android提供的四大组件之一,我们可以认为它是一个没有用户界面、开发者创建的组件,它可以用来执行长久的操作,可以超出单个Activity的范围。Service可以作为客户端/服务器开发模式的服务器,通过进程间通信(IPC)提供远程调用服务。

如果一个任务需要使用worker线程,可能会影响应用的响应速度和性能,而这个任务对处理时间并不敏感,那就考虑使用Service,在主要应用程序和任何单独的Activity生命周期外处理任务。

常见应用

天气、电子邮件或者社交网络的App,可以定期检查网络上更新的服务。

一个游戏,可以在用户需要的时候创建一个Service下载并处理相关内容。

一个照片或者多媒体应用保持数据的在线同步。

一个视频编辑软件,可以将繁重的处理工作放置在服务队列,降低系统能耗。

一个新闻应用,可以实现预加载内容的服务,当用户启动时,提前下载相应新闻,提高性能和相应能力。

开启Service的两种方式:

startService(Intent serviceIntent) 一个组件可以通过startService的方式来开启Service服务。这种方式开启的Service可以调用stopSelf()来停止服务,也可以通过在其他组件中调用stopService()方法来停止。

Intent serviceIntent = new Intent(this, FirstStartedService.class);

startService(serviceIntent);

FirstStartedService就是开启的继承自Service的服务。

public class FirstStart

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值