Android的Service应用程序组件基本编写方法
更新时间:2012年12月10日 15:55:24 作者:
Service是一个android 系统中的应用程序组件,它跟Activity的级别差不多,但是他没有图形化界面,不能自己运行,只能后台运行,Service通常用来处理一些耗时比较长的操作
Service是什么
Service是一个android 系统中的应用程序组件,它跟Activity的级别差不多,但是他没有图形化界面,不能自己运行,只能后台运行,并且可以和其他组件进行交互如更新ContentProvider,Intent以及系统的通知等等。其启动方式有两种:context.startService() 和 context.bindService()。Service通常用来处理一些耗时比较长的操作。
Service的编写
创建一个类(这里为FirstService)继承android.app.Service,并覆盖以下方法:
onBind(Intent intent) Return the communication channel to the service.
onCreate() Called by the system when the service is first created.
onStartCommand(Intent intent, int flags, int startId) Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.
onDestroy() Called by the system to notify a Service that it is no longer used and is being removed.
AndroidManifest.xml文件中添加service配置
<service android:name=".FirstService"></service>
在Activity中启动和停止Service的点击事件的编写
class StartServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
startService(intent);
}
}
class StopServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
stopService(intent);
}
}
Service是一个android 系统中的应用程序组件,它跟Activity的级别差不多,但是他没有图形化界面,不能自己运行,只能后台运行,并且可以和其他组件进行交互如更新ContentProvider,Intent以及系统的通知等等。其启动方式有两种:context.startService() 和 context.bindService()。Service通常用来处理一些耗时比较长的操作。
Service的编写
创建一个类(这里为FirstService)继承android.app.Service,并覆盖以下方法:
onBind(Intent intent) Return the communication channel to the service.
onCreate() Called by the system when the service is first created.
onStartCommand(Intent intent, int flags, int startId) Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.
onDestroy() Called by the system to notify a Service that it is no longer used and is being removed.
AndroidManifest.xml文件中添加service配置
复制代码 代码如下:
<service android:name=".FirstService"></service>
在Activity中启动和停止Service的点击事件的编写
复制代码 代码如下:
class StartServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
startService(intent);
}
}
class StopServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
stopService(intent);
}
}
您可能感兴趣的文章:
- Android应用程序四大组件之使用AIDL如何实现跨进程调用Service
- Android开发中Button组件的使用
- Android Jetpack架构组件 ViewModel详解
- Android ListView UI组件使用说明
- android自定义组件实现仪表计数盘
- Android中butterknife的使用与自动化查找组件插件详解
- Android开发之组件GridView简单使用方法示例
- Android列表组件ListView使用详解之动态加载或修改列表数据
- Android四大组件之Service详解
- Android框架组件Lifecycle的使用详解
- Android UI新组件学习和使用
- 详解Android的四大应用程序组件
相关文章
详解 Android中Libgdx使用ShapeRenderer自定义Actor解决无法接收到Touch事件的问题
这篇文章主要介绍了详解 Android中Libgdx使用ShapeRenderer自定义Actor解决无法接收到Touch事件的问题的相关资料,希望通过本文能帮助到大家解决这样的问题,需要的朋友可以参考下2017-09-09
Android Navigation TabBar控件实现多彩标签栏
这篇文章主要为大家详细介绍了Android Navigation TabBar控件实现多彩标签栏的相关代码,感兴趣的小伙伴们可以参考一下2016-05-05
Android基于CountDownTimer实现倒计时功能
这篇文章主要介绍了Android基于CountDownTimer实现倒计时功能,简单分析了基于CountDownTimer类实现倒计时功能的技巧,需要的朋友可以参考下2015-12-12
Android setButtonDrawable()的兼容问题解决办法
这篇文章主要介绍了Android setButtonDrawable()的兼容问题解决办法的相关资料,需要的朋友可以参考下2017-03-03
Android在fragment中编写toobar的步骤详解
这篇文章主要介绍了Android在fragment中编写toobar,本文分步骤通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-01-01


最新评论