Android-插件化二插桩实现Service的加载
2022-03-09 本文已影响0人
何以邀微微
本篇是在实现Activity的基础上做的,插件apk的加载这块没有变化,所以本篇之介绍关于Service加载的内容
1.宿主apk内定义插桩Service
public class ProxyService extends Service {
public static final String KEY_SERVICE_NAME="serviceName";
String serviceName;
MSInterfaceService mInterfaceService;
@Nullable
@Override
public IBinder onBind(Intent intent) {
init(intent);
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mInterfaceService==null){
init(intent);
}
return mInterfaceService.onStartCommand(intent, flags, startId);
}
private void init(Intent intent) {
serviceName=intent.getStringExtra(KEY_SERVICE_NAME);
try {
Class<?> serviceClass = PluginLoadManager.getInstance().getDexClassLoader().loadClass(serviceName);
Constructor<?> constructor = serviceClass.getConstructor();
Object serviceObject = constructor.newInstance();
if (serviceObject instanceof MSInterfaceService){
mInterfaceService= (MSInterfaceService) serviceObject;
mInterfaceService.attach(this);
mInterfaceService.onCreate();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
if (mInterfaceService!=null){
mInterfaceService.onDestroy();
}
super.onDestroy();
}
@Override
public void onLowMemory() {
if (mInterfaceService!=null){
mInterfaceService.onLowMemory();
}
super.onLowMemory();
}
@Override
public boolean onUnbind(Intent intent) {
mInterfaceService.onUnbind(intent);
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
mInterfaceService.onRebind(intent);
super.onRebind(intent);
}
}
上面是定义在宿主页面的Service插桩,核心部分是init方法,介绍如下:
- 反射拿到插件Service字节码(注意这里使用的DexClassLoader)
- 调用newInstance方法进行实例化
- 注入onCreate方法
最后,在清单文件进行Service注册
2.定义Service标准
public interface MSInterfaceService {
public void onCreate();
public void onStart(Intent intent, int startId);
public int onStartCommand(Intent intent, int flags, int startId);
public void onDestroy();
public void onConfigurationChanged(Configuration newConfig);
public void onLowMemory();
public void onTrimMemory(int level);
public IBinder onBind(Intent intent);
public boolean onUnbind(Intent intent);
public void onRebind(Intent intent);
public void onTaskRemoved(Intent rootIntent);
public void attach(Service proxyService);
}
这部分是协议的功能,主要跟Service的方法进行对齐,有特殊需求可以增加。
3.插件apk部分
public class BaseService extends Service implements MSInterfaceService {
public static final String TAG = BaseService.class.getSimpleName();
private Service that;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void attach(Service proxyService) {
this.that=proxyService;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onStartCommand");
return Service.START_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onDestroy");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onConfigurationChanged");
}
@Override
public void onLowMemory() {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onLowMemory");
}
@Override
public void onTrimMemory(int level) {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onTrimMemory");
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onUnbind");
return false;
}
@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onRebind");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onTaskRemoved");
}
}
定义BaseService,在这里主要就一个功能注入that实例。
public class PluginService extends BaseService {
public PluginService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"onCreate");
}
}
实现一个PluginService,这里只增加了一些打印的功能。
public class MainActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View viewById = findViewById(R.id.image);
viewById.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(new Intent(that,PluginService.class));
}
});
}
}
在点击时间里启动服务,这里的startService方法需要重写,不能直接调用系统的startService。
class BaseActivity extends AppCompatActivity implements MSInterfaceActivity {
protected Activity that;
...
@Override
public ComponentName startService(Intent service) {
Intent newIntent = new Intent();
newIntent.putExtra("serviceName",service.getComponent().getClassName());
return that.startService(newIntent);
}
...
}
这部分其他内容都已经省略,其他内容可以查看上一篇,这个只增加了对startService重写的部分逻辑。
这样就完成了对插件中Service的启动,至于前台服务等内容可以相应的增加,经过测试完美运行,并打印日志。