占位式插件化框架—Service通信

如果没有看过占位式插件化框架—Activity通信的请先看这篇文章,因为这篇文章是在它的基础上写的。
思考:插件p.apk中PluginActivity怎么启动和关闭同是插件包中的TestService?

通过占位式插件化框架—Activity通信应该可以想到怎么实现:

  • 先写一个标准服务的接口ServiceInterface
  • 在插件中写一个服务的基类BaseService,BaseService继承Service和实现ServiceInterface。创建TestService类,TestService类继承BaseService。
  • 在主apk中创建一个服务的代理类(ProxyService),用于调用插件服务的方法。

public interface ServiceInterface {
    /**
     * 把宿主(app)的环境  给  插件
     *
     * @param appService
     */
    void insertAppContext(Service appService);
    void onCreate();
    int onStartCommand(Intent intent, int flags, int startId);
    void onDestroy();
    boolean onUnbind(Intent intent);
    IBinder onBind(Intent intent);
}

public class BaseService extends Service implements ServiceInterface {
    public Service appService;
    @Override
    public void insertAppContext(Service appService) {
        this.appService = appService;
    }
    @Override
    public void onCreate() {
    }
    @SuppressLint("WrongConstant")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return 0;
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onDestroy() {
    }
}
public class TestService extends BaseService {

    public static Boolean Run = true;
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(appService, "启动插件TestService", Toast.LENGTH_LONG).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (Run) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        Log.e("migill", "插件里面的服务TestService开启了一个线程 正在执行中...");
                    }
                }
                Log.e("migill", "插件里面的服务TestService开启的线程执行结束");
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public void onDestroy() {
        Toast.makeText(appService, "关闭插件TestService", Toast.LENGTH_LONG).show();
        Run = false;
        Log.e("migill", "插件里面的服务TestService执行onDestroy()");
        super.onDestroy();
    }
}

TestService的onStartCommand()方法开启了一个线程,在调用onDestroy()的时候线程的run()方法就会执行结束。
接下来就是要增加启动和关闭服务的按钮,点击后要调用宿主的启动和关闭服务的方法。

public class BaseActivity extends Activity implements ActivityInterface {
    .......
    @Override
    public ComponentName startService(Intent service) {
        Intent intentNew = new Intent();
        Log.e("migill", "BaseActivity startService className:" + service.getComponent().getClassName());
        intentNew.putExtra("className", service.getComponent().getClassName());
        return appActivity.startService(intentNew);
    }

    @Override
    public boolean stopService(Intent name) {
        Intent intentNew = new Intent();
        Log.e("migill", "BaseActivity stopService className:" + name.getComponent().getClassName());
        intentNew.putExtra("className", name.getComponent().getClassName());
        return appActivity.stopService(intentNew);
    }
}

在BaseActivity中增加启动和关闭服务的方法。startService()和stopService()两个方法都是调用宿主中的方法。

public class PluginActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plugin);//执行的是BaseActivity中的setContentView方法
        Log.e("migill", "我是插件PluginActivity");
        .......
        findViewById(R.id.bt_start_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startService(new Intent(appActivity, TestService.class));
            }
        });

        findViewById(R.id.bt_stop_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(new Intent(appActivity, TestService.class));
            }
        });
    }
}

点击启动服务的按钮,调用的是BaseActivity中的startService()。
点击停止服务的按钮,调用的是BaseActivity中的stopService()。
编译apk,重命名为p.apk,并放入 /storage/emulated/0/Android/data/com.migill.pluginproject/files/这个目录下。

public class ProxyActivity extends Activity {
......
    @Override
    public ComponentName startService(Intent service) {
        String className = service.getStringExtra("className");
        Log.e("migill", "ProxyActivity startService className : " + className);
        Intent intent = new Intent(this, ProxyService.class);
        intent.putExtra("className", className);
        return super.startService(intent);
    }

    @Override
    public boolean stopService(Intent name) {
        String className = name.getStringExtra("className");
        Log.e("migill", "ProxyActivity stopService className : " + className);
        Intent intent = new Intent(this, ProxyService.class);
        intent.putExtra("className", className);
        return super.stopService(intent);
    }
}

这两个方法都是开启和关闭代理的服务。

public class ProxyService extends Service {
    ServiceInterface serviceInterface;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("migill", "ProxyService onCreate()");
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String className = intent.getStringExtra("className");
        Log.e("migill", "ProxyService onStartCommand() className:" + className);

        try {
            Class mTestServiceClass = PluginManager.getInstance(this).getClassLoader().loadClass(className);
            Object mTestService = mTestServiceClass.newInstance();
            serviceInterface = (ServiceInterface) mTestService;
            //注入组件环境
            serviceInterface.insertAppContext(this);
            serviceInterface.onStartCommand(intent, flags, startId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public void onDestroy() {
        Log.e("migill", "ProxyService onDestroy()");
        if (serviceInterface != null)
            serviceInterface.onDestroy();
        super.onDestroy();
    }
}

启动ProxyService服务的时候会走onStartCommand()生命周期方法,它就会根据全类名实例化对象,并调用serviceInterface.onStartCommand()。
关闭ProxyService服务的时候会走onDestroy()方法,并调用serviceInterface.onDestroy()。


你可能感兴趣的:(占位式插件化框架—Service通信)