总结自Android四大应用组件之Service篇
Android四大应用组件之Activity篇https://www.jianshu.com/p/d22981a8ad0e
Android四大应用组件之Service篇https://www.jianshu.com/p/f7d57803b86e
Android四大应用组件之ContentProvider篇https://www.jianshu.com/p/5f783f3db22d
Android四大应用组件之BroadcastReceiver篇https://www.jianshu.com/p/e6283dec21c7
理论概述
Service是一个应用组件,它用来在后台完成一个时间跨度比较大的工作且没有关联任何界面
默认运行在主线程(UI线程)。耗时工作放在子线程中执行。
即使应用退出,服务也不会停止。
功能:访问网络、播放音乐、文件IO、大数据量的数据库操作
本地服务Local Service:Service对象与Service启动者在同一个进程
远程服务Remoe Service:Service对象与Service启动者不在同一个进程
启动与停止Service
方式一:一般启动
context.startService(Intent intent)
context.stopService(Intent intent)


Intent intent = new Intent(this,MyService.class);
startService(intent);
Log.d("TAG","启动");
Intent intent = new Intent(this,MyService.class);
stopService(intent);
Log.d("TAG","销毁");
//目前先省略其他生命周期方法
点击启动---》
MyService()
MyService onCreate()
点击销毁---》
MyService destroy()
方式二:绑定启动
context.bindService(Intent intent,ServiceConnection con)
context.unbindService(ServiceConnection con)
startService只负责启动并不绑定(产生连接)
Intent intent = new Intent(this,MyService.class);
ServiceConnection conn;
if(conn == null){
conn = new ServiceConnection(){
@Override
public void onServiceDisconnected(ConponentName name){
Log.d("TAG","onServiceDisconnected");
}
@Override
public void onServiceConnected(ConponentName name,IBinder service){
Log.d("TAG","onServiceConnected");
}
}
bindService(intent,conn,Context.BIND_AUTO_CREATE);
Log.d("TAG","bindService");
}else{
Log.d("TAG","已经绑定");
}
if(conn!=null){
unbindService(conn );
conn=null;
Log.d("TAG","unbindService");
}else{
Log.d("TAG","还未绑定");
}
点击bind---》
MyService()
MyService onCreate()
MyService onBind()
MyService onServiceConnected
点击unbind---》
unbindService
MyService destroy()//最后一个client unbind后会自动onUnbind()-->onDestroy()
在Activity退出之前(onDestroy)要调用unbind,否则会泄漏
Service生命周期

1、startService()只有第一次会调用onCreate之后启动会直接走onStartcommand()
2、所有的Clients调用unBindService后,Service会调用onUnbind()-->onDestroy()!!
点击startService()--->
MyService 构造方法
MyService onCreate()
MyService onStartCommand()
再次点击startService()--->
MyService onStartCommand()
点击stopService()--->
MyService onDestroy()
2点击bindService(intent,conn)--->
MyService 构造方法
MyService onCreate()
MyService onBind()
onServiceConnected()
点击unbindService(conn)//当前只有1个相连--->
MyService onUnBind()
MyService onDestroy()
远程服务

另一个应用的服务端:

<service android:name="com.xxx1.MyRemoteService">
<intent-filter>
<action android:name="com.xxx1.MyRemoteService.Action">
</intent-filter>
</service>
public class MyRemoteService extends Service{
@Override
public IBinder onBind(Intent intent){
return new StudentService();
}
//处理Student相关的业务逻辑类
class StudentServece extend IStudentService.Stub{
@Override
public student getStudentById(int id) throws RemoteException{
return new Student(id,"TOM",10000);
}
}
}
服务端定义AIDL接口+Student类
服务端创建IStudentService.aidl(改名为aidl)
package....xxx1...;
import com.xxx1.Student;//必须导包
intenrface IStudentService{
Student getStudentById(int id);
}
public class Student implements Parcelable{
private int id;
private String name;
private double price;
public Student (int id,String name,double price){
super();
this.id=id;
this.name=name;
rhis.price = price;
}
...// get/set /toString 方法略
// Parcelable方法
@Override
public void writeToParcel(Parcel dest,int flags){
dest.wrteInt(id);
dest.wrteString(name);
dest.wrteDouble(price);
}
//
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>(){
//解包
@Override
public Student createFromParcel(Parcel source){
//必须按照写的顺序读(先int 后string 、float)
int id = source.readInt();
String name=source.readString();
double price = source.readDouble();
return new Student(id,name,price);
}
//返回一个指定大小的对象容器
@Override
public Student[] newArray(int size){
return new Student[size];
}
}
}
服务端创建Student.aidl
package com.xxx1...;
parcelable Student;
另一个应用的客户端:
绑定远程服务---》调用远程服务方法---》unbind
复制Student.java、IStudentService.aidl、Student.aidl而且包名不能改

ServiceConnection conn;
IStudentService studentService;
public void bindR(){
Intent intent = new Intent("com.xxx1.MyRemoteService.Action");
if(conn == null){
conn = new ServiceConnection(){
@Override
public void onServiceDisconnected(ConponentName name){
Log.d("TAG","onServiceDisconnected");
}
@Override
public void onServiceConnected(ConponentName name,IBinder service){
Log.d("TAG","onServiceConnected");
studentService=IStudentService.Stub.asInterface(service);
}
}
bindService(intent,conn,Context.BIND_AUTO_CREATE);
Log.d("TAG","bindService");
}else{
Log.d("TAG","已经绑定");
}
}
public void useR(){
if(studentService!=null){
Student st = studentService.getStudentById(1);
}
}
public void unbindR(){
if(conn!=null){
unbindService(conn);
conn=null;
studentService=null;
Log.d("TAG","unbindService");
}else{
Log.d("TAG","还未绑定");
}
}
网友评论