Service
1.什么是Service
Service也是Android四大组件之一, 可以在后台长期运行, 没有界面.
2.定义Service
定义类继承Service, 在清单文件中声明<service>节点. 如果需要使用隐式意图启动, 可以配置<intent-filter>和<action>
3.启动停止Service
调用startService()方法传入一个Intent对象可以启动Service, 调用onCreate()和onStart()方法, onCreate()只有第一次调用时执行
调用stopService()方法传入一个Intent可以停止Service, 调用onDestroy()方法
4.绑定Service
调用bindService()方法绑定一个服务, 这里会调用Service中的onBind()方法
bindService()方法需要传入一个ServiceConnection, 当onBind()返回一个IBinder的时候, 会调用ServiceConnection类中的onServiceConnected()
在Service中为了能让绑定其的客户端调用到service中的业务方法, 所以onBind()返回出去的IBinder就需要能调用业务方法
我们可以自定义一个类继承Binder, 并且在类中定义一些方法, 调用Service中的方法. 这样onBind()方法返回出去的对象就能调用Service中的方法了
绑定处的onServiceConnected(ComponentName name, IBinder service)方法会得到一个IBinder参数, 这个IBinder是onBind()返回对象的父类, 不能直接调用子类方法
为了解决这个问题, 我们使用了aidl技术
在Service中定义一个接口, 改为aidl文件, 这时会在R文件中生成一个java文件,自定义的类继承自动生成的Stub类(最终是继承Stub,而不是Binder)
Service清单文件
1.什么是Service
Service也是Android四大组件之一, 可以在后台长期运行, 没有界面.
2.定义Service
定义类继承Service, 在清单文件中声明<service>节点. 如果需要使用隐式意图启动, 可以配置<intent-filter>和<action>
3.启动停止Service
调用startService()方法传入一个Intent对象可以启动Service, 调用onCreate()和onStart()方法, onCreate()只有第一次调用时执行
调用stopService()方法传入一个Intent可以停止Service, 调用onDestroy()方法
4.绑定Service
调用bindService()方法绑定一个服务, 这里会调用Service中的onBind()方法
bindService()方法需要传入一个ServiceConnection, 当onBind()返回一个IBinder的时候, 会调用ServiceConnection类中的onServiceConnected()
在Service中为了能让绑定其的客户端调用到service中的业务方法, 所以onBind()返回出去的IBinder就需要能调用业务方法
我们可以自定义一个类继承Binder, 并且在类中定义一些方法, 调用Service中的方法. 这样onBind()方法返回出去的对象就能调用Service中的方法了
绑定处的onServiceConnected(ComponentName name, IBinder service)方法会得到一个IBinder参数, 这个IBinder是onBind()返回对象的父类, 不能直接调用子类方法
为了解决这个问题, 我们使用了aidl技术
在Service中定义一个接口, 改为aidl文件, 这时会在R文件中生成一个java文件,自定义的类继承自动生成的Stub类(最终是继承Stub,而不是Binder)
绑定处的onServiceConnected()方法使用Stub.asInterface(service)方法将IBinder转为接口类型, 即可调用业务方法
远程调用Service步骤:
1.将定义的接口文件后缀名改为aidl,分别放在两个应用的相同包下
2.在service应用中自定义类继承Stub,并在类中写调用业务的方法。
3.在客户端中
private ServiceConnection conn = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
}
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("绑定成功,得到IBinder");
InvokeInterface ii = Stub.asInterface(service);//调用此方法获取实现自定义接口的自定义类
System.out.println("ii---->"+ii);
}
};
Service
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.xxc.service.InvokeInterface.Stub;
public class RemoteService extends Service {
//自定义类继承Stub,在类中定义需要被远程调用的业务方法
private class MyBinder extends Stub {
public String sayHello(String name) {
return RemoteService.this.sayHello(name);
}
}
public String sayHello(String name){
return "Hello "+name;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("Remote onCreate");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
System.out.println("Remote onStart");
}
@Override
public IBinder onBind(Intent intent) {
System.out.println("Remote onBind");
return new MyBinder();//绑定service的时候返回自定义类
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("Remote onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Remote onDestroy");
}
}
Service清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxc.remoteservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- 注册Service -->
<service android:name=".RemoteService">
<intent-filter>
<action android:name="com.xxc.service.RemoteService"></action>
</intent-filter>
</service>
</application>
</manifest>
远程调用Service的客户端
import com.xxc.service.InvokeInterface;
import com.xxc.service.InvokeInterface.Stub;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
public class MainActivity extends Activity {
private InvokeInterface ii;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private ServiceConnection conn = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
}
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("绑定成功,得到IBinder");
ii = Stub.asInterface(service);
System.out.println("ii---->"+ii);
}
};
public void onClick(View view) throws RemoteException{
//远程调用Service
Intent intent = new Intent("com.xxc.service.RemoteService");
switch (view.getId()) {
case R.id.startBT:
startService(intent);//启动服务
break;
case R.id.bindBT:
bindService(intent, conn, BIND_AUTO_CREATE);//绑定服务 第三个参数表示,如果此时没启动服务就先启动服务
break;
case R.id.invokeBT:
System.out.println("ii在调用---->"+ii);
System.out.println(ii.sayHello("pkq"));//调用服务的业务方法
break;
case R.id.unbindBT:
unbindService(conn);//解绑服务
break;
case R.id.stopBT:
stopService(intent);//停止服务
break;
}
}
}
Service和客户端两个应用中都要有的aidl文件(把修饰符全部去掉)
package com.xxc.service;
interface InvokeInterface {
String sayHello(String name);
}