Android-插件化二插桩实现Service的加载

Android-插件化一插桩实现Activity的加载

本篇是在实现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的启动,至于前台服务等内容可以相应的增加,经过测试完美运行,并打印日志。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值