当前位置: 首页 > news >正文

网站建设免费软件有哪些seo网站推广方案

网站建设免费软件有哪些,seo网站推广方案,施工企业工程施工科目,wordpress占资源Android Service的生命周期,两种启动方法,有什么区别 Android Service是一种后台组件,用于在后台执行长时间运行的任务,而无需与用户界面进行交互。Service具有自己的生命周期,其主要包含以下几个状态:创建…

Android Service的生命周期,两种启动方法,有什么区别

Android Service是一种后台组件,用于在后台执行长时间运行的任务,而无需与用户界面进行交互。Service具有自己的生命周期,其主要包含以下几个状态:创建(Created)、启动(Started)、绑定(Bound)和销毁(Destroyed)。现在,让我详细展开Android Service的生命周期和两种启动方法以及它们之间的区别:

Android Service的生命周期:

  • 创建(Created):
    在调用onCreate()方法后,Service会进入Created状态。在这个阶段,Service被创建并初始化,但尚未开始运行。

  • 启动(Started):
    当调用onStartCommand()方法后,Service会进入Started状态。在这个阶段,Service已经开始运行,并且会在后台执行耗时任务。Service将一直运行直到调用stopSelf()或者其他组件通过调用stopService()停止该Service。

  • 绑定(Bound):
    Service可以通过bindService()方法绑定到其他组件(例如Activity)上。当Service被绑定后,与之绑定的组件可以与Service进行通信,并调用Service中的方法。在这个阶段,Service仍然在运行,但可以通过调用unbindService()解除绑定。

  • 销毁(Destroyed):
    当Service不再被使用并且没有被绑定时,系统会调用onDestroy()方法,将Service销毁并释放资源。

两种启动方法及区别:

启动Service(Started Service):

  • 使用startService(Intent intent)方法启动Service。
  • Service会一直运行直到调用stopSelf()或者其他组件通过stopService()停止该Service。
  • 不需要与调用者进行绑定,Service可以在后台执行耗时任务。

绑定Service(Bound Service):

  • 使用bindService(Intent intent, ServiceConnection connection, int flags)方法绑定Service。
  • Service和调用者之间建立了绑定关系,调用者可以通过ServiceConnection与Service进行通信。
  • 当所有与之绑定的组件都解除绑定时,系统会销毁Service。

区别:

  • 启动Service适用于执行后台任务,即使调用者退出,Service仍然会继续运行。
  • 绑定Service适用于需要与其他组件进行交互的情况,Service的生命周期由绑定的组件决定。

需要注意的是,绝大多数情况下,Service都应该在后台线程中执行耗时任务,以免阻塞主线程,从而导致应用程序无响应。

代码举例说明

让我们通过代码来演示Android Service的两种启动方法:Started Service和Bound Service。

  • 创建一个继承自Service的Started Service类,例如MyStartedService。
public class MyStartedService extends Service {@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// 在这里执行后台任务,例如下载文件等耗时操作// 注意:不要在主线程执行耗时任务,以免阻塞应用程序// ...// 任务执行完成后,调用stopSelf()停止ServicestopSelf();// 返回START_NOT_STICKY,表示Service在被异常杀死后不会自动重启return START_NOT_STICKY;}@Nullable@Overridepublic IBinder onBind(Intent intent) {// Started Service不支持绑定,所以该方法返回nullreturn null;}
}
  • 在AndroidManifest.xml文件中注册Started Service:
<serviceandroid:name=".MyStartedService"android:enabled="true"android:exported="false" />
  • 在其他组件(例如Activity)中启动Started Service:
Intent serviceIntent = new Intent(this, MyStartedService.class);
startService(serviceIntent);

Bound Service 示例:

  • 创建一个继承自Service的Bound Service类,例如MyBoundService。
public class MyBoundService extends Service {private final IBinder binder = new MyBinder();public class MyBinder extends Binder {MyBoundService getService() {return MyBoundService.this;}}@Nullable@Overridepublic IBinder onBind(Intent intent) {// 返回Binder对象,允许调用者与Service进行通信return binder;}// 在Bound Service中可以提供一些公共方法供调用者调用public void doSomething() {// 在这里执行一些操作}
}
  • 在AndroidManifest.xml文件中注册Bound Service:
<serviceandroid:name=".MyBoundService"android:enabled="true"android:exported="true" />
  • 在其他组件(例如Activity)中绑定Bound Service:
private MyBoundService mBoundService;
private boolean mServiceBound = false;private ServiceConnection serviceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName componentName, IBinder iBinder) {MyBoundService.MyBinder binder = (MyBoundService.MyBinder) iBinder;mBoundService = binder.getService();mServiceBound = true;}@Overridepublic void onServiceDisconnected(ComponentName componentName) {mServiceBound = false;}
};@Override
protected void onStart() {super.onStart();Intent intent = new Intent(this, MyBoundService.class);bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}@Override
protected void onStop() {super.onStop();if (mServiceBound) {unbindService(serviceConnection);mServiceBound = false;}
}

通过上面的代码示例,我们演示了Android中两种启动Service的方法,Started Service通过startService()启动,而Bound Service通过bindService()绑定到其他组件上。Started Service适用于需要在后台执行耗时任务的场景,而Bound Service适用于需要与其他组件进行交互的场景。需要注意的是,Service的生命周期由启动或绑定的组件决定,在不需要使用Service时应及时停止或解绑。

http://www.hkea.cn/news/307322/

相关文章:

  • 网络服务商是指什么网站优化排名工具
  • 网站优化的分析比较好的品牌策划公司有哪些
  • 国外比较好的资源网站电商运营推广是做什么的
  • 佛山房地产网站建设seo实战培训王乃用
  • 如何做可以赚钱的网站关键词如何快速排名
  • 深圳品牌做网站公司有哪些百度app推广
  • 重庆建设行业信息网站搜狗登录入口
  • 同仁行业网站建设报价北京做的好的seo公司
  • 陕西自助建站做网站郑州外语网站建站优化
  • 小型企业网站系统cilimao磁力猫最新版地址
  • 铁岭网站建设移动网站广东网站seo
  • 网站模板插件sem和seo
  • 用wordpress制作网站模板沈阳seo
  • 优化一个网站多少钱宜昌网站seo
  • 刚做的网站怎么才能搜索到枸橼酸西地那非片功效效及作用
  • 罗湖区网站公司专业模板建站
  • 哪有备案好的网站国产系统2345
  • 网站开发怎么让别人看到最新营销模式有哪些
  • ssm网站开发源码百度推广多少钱一个月
  • 手游门户网站建设appstore关键词优化
  • 齐河网站开发seo服务内容
  • 北京微信网站建设费用想卖产品怎么推广宣传
  • 网站上线的步骤厦门网站推广公司哪家好
  • 网站做app的软件有哪些百度一下你就知道下载
  • 界面设计的重要性百度seo关键词排名推荐
  • 股票做T网站直播营销
  • 北京手机网站建设公司排名技术优化seo
  • wordpress可爱的主题seo优化教程
  • 自己可以申请网站做外卖吗网站描述和关键词怎么写
  • 公司网站网页设计seo站长工具推广平台