手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android的Service应用程序组件基本编写方法
Android的Service应用程序组件基本编写方法
摘要:Service是什么Service是一个android系统中的应用程序组件,它跟Activity的级别差不多,但是他没有图形化界面,不能自己...

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);

}

}

【Android的Service应用程序组件基本编写方法】相关文章:

Android Service(不和用户交互应用组件)案例分析

Android应用中调用系统软件打开各种各样的文件

Android中Webview自适应屏幕

Android应用程序的调试

Android adb的常用命令

Android Service中方法使用详细介绍

Android实现长按back键退出应用程序的方法

android 为应用程序创建桌面快捷方式技巧分享

android 触屏的震动响应接口调用方法

Android控件系列之ImageView使用方法

精品推荐
分类导航