Service:
service是安卓四大组件之一,是一种可在后台执行长时间运行操作而不提供界面的应用组件,即便用户将应用切换到了后台,服务依旧可以正常运行,服务可由其他应用组件(例如Activity)启动。
AIDL: 接口定义语言
由于在Android中进程间无法共享内存(进程隔离),AIDL就是用于两个进程间通信的工具。
实例分享:
实现进程间的通信,A应用实现一个服务,服务里有两个方法,B应用绑定调用A应用的服务,并调用两个方法sayHello和sayHelloTo,其中sayHelloTo方法为传递复杂数据类型。
方法1:public String sayHello(){
return "你好,我是A应用";
}
方法2: public Personal sayHelloTo(String name,boolean sex,int age){
return new Personal(name,sex,age);
}
相关代码如下:
应用A的MainActivity.java:
package com.example.myeverytest;
import androidx.appcompat.app.AppCompatActivity;
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.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public static final String TAG = "MYTAG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//该方法会调用 Service 中的 onCreate() 和 onStartCommand() 方法来启动一个后台 Service,
// 当Service 销毁时直接调用 onDestroy() 方法。
public void startService(View view){
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
public void stopService(View view){
Intent intent = new Intent(MainActivity.this,MyService.class);
stopService(intent);
}
//通过 bindService() 方法启动 Service,则其生命周期受其绑定对象控制。一个 Service 可以同时绑定
到多个对象上,当没有任何对象绑定到 Service 上时,该 Service 会被系统销毁
public void bindService(View view){
Intent intent = new Intent(MainActivity.this,MyService.class);
bindService(intent,con,BIND_AUTO_CREATE);
}
public void sayHello(View view){
if (localService != null){
String hello = null;
try {
hello = localService.sayHello();
} catch (RemoteException e) {
e.printStackTrace();
}
Log.d(TAG, "sayHello: " + hello);
}
}
public void sayHelloTo(View view){
if (localService != null){
Personal person = null;
try {
person = localService.sayHelloTo("张三",true,18);
} catch (RemoteException e) {
e.printStackTrace();
}
Log.d(TAG, "sayHelloTo: " + person.getName());
}
}
public void unBindService(View view){
unbindService(con);
}
ILocalService localService;
private ServiceConnection con = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { //服务连接时回调
Log.d(TAG, "onServiceConnected: ");
localService = ILocalService.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) { //服务解绑成功回调
Log.d(TAG, "onServiceDisconnected: ");
}
};
}
Service有两种使用方式
startService:
生命周期: onCreate → startCommand → onDestroy
Service一旦被启动,在调用者没有调用stopService()的前提下,其生命周期就和调用者的生命周期不再关联。即使调用者被杀掉,Service也可以独立生存。
bindService:
生命周期:onCreate → onBind→onUnBind→ onDestroy
与startService方式不同的是,这种方式,service的生命周期是和调用者关联在一起的,如果调用者进程被终结后,服务便会终止。
应用A的Mysevice.java
package com.example.myeverytest;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import androidx.annotation.Nullable;
public class MyService extends Service {
public static final String TAG = "MYTAG";
@Override