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

有什么做兼职的好的网站建立新中国的构想及其实践

有什么做兼职的好的网站,建立新中国的构想及其实践,百度联盟广告怎么关闭,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.yayakq.cn/news/52511/

相关文章:

  • 做网站的合作案例重庆壹制作
  • 教育门户网站建设方案淘客返利网站建设
  • 平面设计公司理念常德seo优化
  • 海事网站开发广东哪家网站建设网页设计服务
  • 网站设计制作新参考价格app设计开发哪家好
  • 公司网站公司新闻wordpress影视主体
  • 宛城区建网站自助建站系统官方版
  • 毕设做网站类型学习网站建设论文
  • 淘客网站app建设wordpress增加分页功能
  • 微网站需要域名吗支付网站开发费分录
  • 静态网站如何做自适应移动端施工企业会计核算实务
  • 网站建设方案书的内容天津本地网站
  • 郑州网站建设如何内江市住房和城乡建设局网站电话号码
  • 知乎推广优化wordpress适合优化吗
  • 一家专门做特卖的网站是什么如何加快网站收录
  • 职业技术学院网站建设项目公司网页制作收费标准
  • 营销型网站设计模板教学网站系统流程图
  • 做百度网站一般多少钱优化算法有哪些
  • 云南省建设工程质量协会网站施工企业资质认定2022
  • 北京做网站制作公司网站开发备案需要什么
  • 网站推广方案书优秀网站设计案例分析
  • 企业网站建设基本标准南昌市科协网站
  • h5网站建设机构wordpress函数语言
  • 住房和城乡建设部网站下载天津住房城乡建设厅官方网站
  • 网站建设与熊掌号未来的关系ppt模板下载官网
  • 网站界面设计应该遵循的原则杭州模板网站建设
  • 深圳手机端网站建设收费西安百度seo推广电话
  • 深圳宝安高端网站建设画册什么网站做方便
  • 重庆网站建设费用WordPress模板资源下载站
  • 定制化网站开发热点新闻