Android基础(十) Service 服务
2013-04-21 21:04
323 查看
1.什么时Service
Service也是Android的四大组件之一。
Service时一种可以长期在后台运行,没有界面的组件。由其他组件调用开始运行。
2.定义Service
需要定义一个类继承Service,而且要在清单文件AndroidManifest.xml定义<service>节点;如果需要使用隐式意图启动,还需要配置<intent-filter>和<action>。
3.启动和停止Service
调用startService()方法,并传入一个Intent对象可以启动Service。Service启动时会调用onCreate()和onStart()方法,而且onCreate()方法仅在第一次调用时执行。
调用stopService()并传入一个Intent对象可以停止Service,停止时会调用onDestroy()方法。
在Activity中也可以使用bindService(Intent intent)来开启一个服务,但是该Service会随着Activity的销毁而销毁。
main.xml
4.绑定本地Service
使用bindService()方法来绑定服务,需要传入一个自定义的ServiceConnection用来接收IBinder
定义一个业务接口,其中定义需要使用的方法
Service中有一个IBinder继承Binder并实现业务接口,在onBind方法中返回
调用端将IBinder转为接口类型,调用接口中的方法即可调用到服务中的方法。
Activity调用Service中的方法示例:
Activity通过接口调用Service中的方法示例:
5.绑定远程Service(AIDL:Android Interface Definition Language)
远程绑定服务时无法通过同一个接口来调用方法,这时就要使用AIDL技术
将接口的扩展名改为.aidl,去掉所有的权限修饰符
gen文件夹下会自动生成同名的接口
将Service中自定义的IBinder类改为继承接口中的Stub
ServiceConnection中返回的IBinder是代理对象,不能使用强制转换,而是要改用Stub.asInterface()
Service也是Android的四大组件之一。
Service时一种可以长期在后台运行,没有界面的组件。由其他组件调用开始运行。
2.定义Service
需要定义一个类继承Service,而且要在清单文件AndroidManifest.xml定义<service>节点;如果需要使用隐式意图启动,还需要配置<intent-filter>和<action>。
3.启动和停止Service
调用startService()方法,并传入一个Intent对象可以启动Service。Service启动时会调用onCreate()和onStart()方法,而且onCreate()方法仅在第一次调用时执行。
调用stopService()并传入一个Intent对象可以停止Service,停止时会调用onDestroy()方法。
在Activity中也可以使用bindService(Intent intent)来开启一个服务,但是该Service会随着Activity的销毁而销毁。
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/startBT" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="开启服务" /> <Button android:id="@+id/stopBT" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="停止服务" /> </LinearLayout>
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View view) { Intent intent = new Intent(this, MyService.class); switch (view.getId()) { case R.id.startBT: startService(intent); break; case R.id.stopBT: stopService(intent); break; } } }
public class MyService extends Service { private boolean isRunning; @Override public void onCreate() {// 创建时执行 super.onCreate(); System.out.println("onCreate"); } @Override public void onStart(Intent intent, int startId) {// 启动时执行 super.onStart(intent, startId); System.out.println("onStart"); isRunning = true; new Thread() { public void run() { for (int i = 0; isRunning; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }; }.start(); } @Override public void onDestroy() {// 停止时执行 super.onDestroy(); System.out.println("onDestroy"); isRunning = false; } @Override public IBinder onBind(Intent intent) {// 绑定服务时执行 System.out.println("onBind"); return null; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xxx.service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService"/> </application> </manifest>
4.绑定本地Service
使用bindService()方法来绑定服务,需要传入一个自定义的ServiceConnection用来接收IBinder
定义一个业务接口,其中定义需要使用的方法
Service中有一个IBinder继承Binder并实现业务接口,在onBind方法中返回
调用端将IBinder转为接口类型,调用接口中的方法即可调用到服务中的方法。
Activity调用Service中的方法示例:
Activity通过接口调用Service中的方法示例:
5.绑定远程Service(AIDL:Android Interface Definition Language)
远程绑定服务时无法通过同一个接口来调用方法,这时就要使用AIDL技术
将接口的扩展名改为.aidl,去掉所有的权限修饰符
gen文件夹下会自动生成同名的接口
将Service中自定义的IBinder类改为继承接口中的Stub
ServiceConnection中返回的IBinder是代理对象,不能使用强制转换,而是要改用Stub.asInterface()
相关文章推荐
- Android 服务(service)的基础用法
- android基础笔记:服务(Service)跨进程调用Service(AIDL Service)
- 详解Android中Service服务的基础知识及编写方法
- android四大组件基础(五),绑定Service服务(含IPC)
- Android应用组件之服务(Service)---概述及基础
- Android学习篇章35-Service基础-广播开机启动服务
- Android基础------Service服务
- Android基础-Service(服务)
- android基础笔记:服务(Service) 采用接口的方式调用本地服务的方法
- android基础笔记:服务(Service):基础知识及本地服务的实现
- Android基础之Service(1)—本地服务
- 【android基础】进程内后台服务 Service
- Android Service完全解析,关于服务你所需知道的一切(下)
- android学习:service 绑定,与服务进行通信
- Android Service 服务(一)—— Service
- [android基础知识] 之四:TelephonyManager service的使用实例
- Android重要组件之一 Service 服务讲解学习(一)
- Android Service学习之AIDL, Parcelable和远程服务
- 47.android服务service-代码注册广播接收者
- ionic准备之angular基础———服务provider 和 factory和service(9)