如果沒(méi)有看過(guò)占位式插件化框架—Activity通信的請(qǐng)先看這篇文章届巩,因?yàn)檫@篇文章是在它的基礎(chǔ)上寫的阔馋。
思考:插件p.apk中PluginActivity怎么啟動(dòng)和關(guān)閉同是插件包中的TestService?
通過(guò)占位式插件化框架—Activity通信應(yīng)該可以想到怎么實(shí)現(xiàn):
- 先寫一個(gè)標(biāo)準(zhǔn)服務(wù)的接口ServiceInterface
- 在插件中寫一個(gè)服務(wù)的基類BaseService咬最,BaseService繼承Service和實(shí)現(xiàn)ServiceInterface鹅巍。創(chuàng)建TestService類,TestService類繼承BaseService请敦。
- 在主apk中創(chuàng)建一個(gè)服務(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, "啟動(dòng)插件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開(kāi)啟了一個(gè)線程 正在執(zhí)行中...");
}
}
Log.e("migill", "插件里面的服務(wù)TestService開(kāi)啟的線程執(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()方法開(kāi)啟了一個(gè)線程岖常,在調(diào)用onDestroy()的時(shí)候線程的run()方法就會(huì)執(zhí)行結(jié)束。
接下來(lái)就是要增加啟動(dòng)和關(guān)閉服務(wù)的按鈕葫督,點(diǎn)擊后要調(diào)用宿主的啟動(dòng)和關(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中增加啟動(dòng)和關(guān)閉服務(wù)的方法板惑。startService()和stopService()兩個(gè)方法都是調(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));
}
});
}
}
點(diǎn)擊啟動(dòng)服務(wù)的按鈕偎快,調(diào)用的是BaseActivity中的startService()洒放。
點(diǎn)擊停止服務(wù)的按鈕,調(diào)用的是BaseActivity中的stopService()滨砍。
編譯apk,重命名為p.apk,并放入 /storage/emulated/0/Android/data/com.migill.pluginproject/files/這個(gè)目錄下往湿。
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);
}
}
這兩個(gè)方法都是開(kāi)啟和關(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();
}
}
啟動(dòng)ProxyService服務(wù)的時(shí)候會(huì)走onStartCommand()生命周期方法惋戏,它就會(huì)根據(jù)全類名實(shí)例化對(duì)象领追,并調(diào)用serviceInterface.onStartCommand()。
關(guān)閉ProxyService服務(wù)的時(shí)候會(huì)走onDestroy()方法响逢,并調(diào)用serviceInterface.onDestroy()绒窑。