如果沒有看過占位式插件化框架—Activity通信的請先看這篇文章,因為這篇文章是在它的基礎(chǔ)上寫的。
思考:插件p.apk中PluginActivity怎么啟動和關(guān)閉同是插件包中的TestService?
通過占位式插件化框架—Activity通信應(yīng)該可以想到怎么實現(xiàn):
- 先寫一個標(biāo)準(zhǔn)服務(wù)的接口ServiceInterface
- 在插件中寫一個服務(wù)的基類BaseService,BaseService繼承Service和實現(xiàn)ServiceInterface。創(chuàng)建TestService類,TestService類繼承BaseService。
- 在主apk中創(chuàng)建一個服務(wù)的代理類(ProxyService),用于調(diào)用插件服務(wù)的方法。
public interface ServiceInterface {
/**
* 把宿主(app)的環(huán)境 給 插件
*
* @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", "插件里面的服務(wù)TestService開啟了一個線程 正在執(zhí)行中...");
}
}
Log.e("migill", "插件里面的服務(wù)TestService開啟的線程執(zhí)行結(jié)束");
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Toast.makeText(appService, "關(guān)閉插件TestService", Toast.LENGTH_LONG).show();
Run = false;
Log.e("migill", "插件里面的服務(wù)TestService執(zhí)行onDestroy()");
super.onDestroy();
}
}
TestService的onStartCommand()方法開啟了一個線程,在調(diào)用onDestroy()的時候線程的run()方法就會執(zhí)行結(jié)束。
接下來就是要增加啟動和關(guān)閉服務(wù)的按鈕,點擊后要調(diào)用宿主的啟動和關(guān)閉服務(wù)的方法。
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中增加啟動和關(guān)閉服務(wù)的方法。startService()和stopService()兩個方法都是調(diào)用宿主中的方法。
public class PluginActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plugin);//執(zhí)行的是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));
}
});
}
}
點擊啟動服務(wù)的按鈕,調(diào)用的是BaseActivity中的startService()。
點擊停止服務(wù)的按鈕,調(diào)用的是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);
}
}
這兩個方法都是開啟和關(guān)閉代理的服務(wù)。
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;
//注入組件環(huán)境
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服務(wù)的時候會走onStartCommand()生命周期方法,它就會根據(jù)全類名實例化對象,并調(diào)用serviceInterface.onStartCommand()。
關(guān)閉ProxyService服務(wù)的時候會走onDestroy()方法,并調(diào)用serviceInterface.onDestroy()。

