EP8-给插件提供Service

给插件提供Service

0x00 Activityable

我们回忆一下插件中是怎么使用Activity的。

  • 1.PluginActivity继承真正的Activity。

  • 2.在PluginActivity中,通过下面的构造器,获得了插件中Plugin.Java的实例。

Constructor<?> pluginMainConstructor = pluginMain.getConstructor();//获取pluginMain的构造方法(pluginMain就是读取的Plugin)
mActivity = (Activityable) pluginMainConstructor
        .newInstance();
  • 3.PluginActivity的onCreate()/onResume()..等等生命周期函数里面,先判断mActivity是不是Plugin(Activityable)的实例,如果是,就调用mActivity对应的生命周期 (****事实上其实这一步判断是否是Activityable的实例的操作是多余的,因为mActivity的定义的时候就是用Activityable定义的*)。
@Override
    protected void onStart() {
        initPluginPath();
        if (mActivity != null && mActivity instanceof Activityable) {
             mActivity.onStart();
        }
        super.onStart();
    }

0x01 Serviceable

那么,类似地,对于Service,我们首先也要通过构造器,获得插件中的PluginService.java的实例:

Constructor<?> serviceConstructor = serviceClass
            .getConstructor(new Class[] {});
    instance = serviceConstructor.newInstance(new Object[] {});
    mPluginService = (Serviceable)serviceConstructor.newInstance();

然后,我们创建一个Serviceable接口:

public interface Serviceable {
    IBinder onBind(Intent intent);

    void onCreate();

    int onStartCommand(Intent intent, int flags, int startId);

    void onDestroy();

    void onConfigurationChanged(Configuration newConfig);

    void onLowMemory();

    void onTrimMemory(int level);

    boolean onUnbind(Intent intent);

    void onRebind(Intent intent);

    void onTaskRemoved(Intent rootIntent);
}

然后,创建一个PluginService.java,作为代理Service:

public class PluginService extends Service {

    protected Serviceable mPluginService;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (mPluginService != null) {
            mPluginService.onConfigurationChanged(newConfig);
        }
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onLowMemory() {
        if (mPluginService != null) {
            mPluginService.onLowMemory();
        }
        super.onLowMemory();
    }

    @Override
    @SuppressLint("NewApi")
    public void onTrimMemory(int level) {
        if (mPluginService != null) {
            mPluginService.onTrimMemory(level);
        }
        super.onTrimMemory(level);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        if (mPluginService != null) {
            mPluginService.onUnbind(intent);
        }
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        if (mPluginService != null) {
            mPluginService.onRebind(intent);
        }
        super.onRebind(intent);
    }
}

最后,我们在插件的PluginService中继承Serviceable,然后覆写各生命周期即可。

嗯,思路大概就是这个样子。具体还没有写完,明天可以试一下。

-NOV30

Reference:
[1] https://my.oschina.net/kymjs/blog/331997

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容