1、我們想要在主APK中啟動(dòng)沒有安裝的插件p.apk的PluginActivity應(yīng)該怎么做呢?
2、插件p.apk中PluginActivity怎么啟動(dòng)同是插件包中的TestActivity?
定義一個(gè)接口
import android.app.Activity;
import android.os.Bundle;
public interface ActivityInterface {
/**
* 把宿主(app)的環(huán)境 給 插件
*
* @param appActivity
*/
void insertAppContext(Activity appActivity);
void onCreate(Bundle savedInstanceState);
void onStart();
void onRestart();
void onResume();
void onDestroy();
void onPause();
void onStop();
}
注意:
- 1、因?yàn)椴寮嗀PK是沒有安裝到手機(jī)上谓媒,所以是無法擁有組件環(huán)境的。
因?yàn)闆]有組件環(huán)境何乎,所以在插件中句惯,就不能使用this。- 2支救、所有關(guān)于操作抢野,組件環(huán)境的地方,都必須使用宿主的環(huán)境各墨。
3指孤、在插件的ActivityAndroidManifest.xml里面不用配置 Activity。
public class BaseActivity extends Activity implements ActivityInterface {
public Activity appActivity;
@Override
public void insertAppContext(Activity appActivity) {
this.appActivity = appActivity;
}
........
public void setContentView(int resId) {
appActivity.setContentView(resId);
}
public View findViewById(int layout) {
return appActivity.findViewById(layout);
}
@Override
public void startActivity(Intent intent) {
Intent intentNew = new Intent();
//className是包名加類名--com.migill.plugin_package.TestActivity
intentNew.putExtra("className", intent.getComponent().getClassName());
appActivity.startActivity(intentNew);
}
}
BaseActivity中的setContentView()贬堵、 findViewById()恃轩、startActivity()方法的實(shí)現(xiàn)都是調(diào)用的宿主appActivity的方法。
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_activity).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(appActivity, TestActivity.class));
}
});
}
}
PluginActivity中的setContentView()黎做、 findViewById()叉跛、startActivity()都是調(diào)用的
BaseActivity中的方法。
public class TestActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Log.e("migill","我是插件TestActivity");
Toast.makeText(appActivity, "我是插件TestActivity", Toast.LENGTH_LONG).show();
}
}
TestActivity中的setContentView()調(diào)用的是BaseActivity中的方法蒸殿。
把插件編譯出來的apk重新命名為p.apk筷厘。
放入 /storage/emulated/0/Android/data/com.migill.pluginproject/files/這個(gè)目錄下。
public class PluginManager {
private static PluginManager pluginManager;
private Context context;
public static PluginManager getInstance(Context context) {
if (pluginManager == null) {
synchronized (PluginManager.class) {
if (pluginManager == null) {
pluginManager = new PluginManager(context);
}
}
}
return pluginManager;
}
public PluginManager(Context context) {
this.context = context;
}
private DexClassLoader dexClassLoader;
private Resources resources;
public void loadPlugin() {
try {
File file = new File(context.getExternalFilesDir(null).getAbsolutePath() + File.separator + "p.apk");
if (!file.exists()) {
Log.e("migill", "插件包宏所,不存在......");
return;
}
String pluginPaht = file.getAbsolutePath();
//1酥艳、現(xiàn)加載插件里面的 class
//dexClassLoader需要一個(gè)緩存目錄 /data/data/當(dāng)前應(yīng)用的包名/pDir
File fileDir = context.getDir("pDir", Context.MODE_PRIVATE);
Log.e("migill", "pluginPaht : " + pluginPaht);
Log.e("migill", "fileDir : " + fileDir.getAbsolutePath());
//Activity class
dexClassLoader = new DexClassLoader(pluginPaht, fileDir.getAbsolutePath(), null, context.getClassLoader());
//2、加載插件里面的layout
//加載資源
AssetManager assetManager = AssetManager.class.newInstance();
// 我們要執(zhí)行此方法楣铁,為了把插件包的路徑添加進(jìn)去 public final int addAssetPath(String path) path是路徑
Method addAssetPathMethod = assetManager.getClass().getMethod("addAssetPath", String.class);
addAssetPathMethod.invoke(assetManager, pluginPaht);//插件包的路徑 pluginPaht
Resources r = context.getResources(); //宿主的資源配置信息
//特殊的 Resource , 加載插件里面的資源的玖雁,Resources
resources = new Resources(assetManager, r.getDisplayMetrics(), r.getConfiguration());
} catch (Exception e) {
e.printStackTrace();
}
}
public ClassLoader getClassLoader() {
return dexClassLoader;
}
public Resources getResources() {
return resources;
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadPlugin(View view) {
PluginManager.getInstance(this).loadPlugin();
}
public void startPluginActivity(View view) {
File file = new File(this.getExternalFilesDir(null).getAbsolutePath() + File.separator + "p.apk");
String path = file.getAbsolutePath();
Log.e("migill", "MainActivity path:" + path);
//獲取插件包里面的Activity
PackageManager packageManager = this.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
ActivityInfo activityInfo = packageInfo.activities[0];
Log.e("migill", "MainActivity activityInfoName:" + activityInfo.name);
//占位代理Activity
Intent intent = new Intent(this, ProxyActivity.class);
intent.putExtra("className", activityInfo.name);
startActivity(intent);
}
}
public class ProxyActivity extends Activity {
@Override
public Resources getResources() {
return PluginManager.getInstance(this).getResources();
}
@Override
public ClassLoader getClassLoader() {
return PluginManager.getInstance(this).getClassLoader();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String className = getIntent().getStringExtra("className");
Log.e("migill", "ProxyActivity onCreate() " + className);
try {
Class mPlaginActivityClass = getClassLoader().loadClass(className);
//實(shí)例化 插件包里面的 Activity
Constructor constructor = mPlaginActivityClass.getConstructor(new Class[]{});
Object mPluginActivity = constructor.newInstance(new Object[]{});
ActivityInterface activityInterface = (ActivityInterface) mPluginActivity;
//注入宿主
activityInterface.insertAppContext(this);
Bundle bundle = new Bundle();
bundle.putString("appName", "我是宿主傳遞過來的信息");
//執(zhí)行插件里面的onCreate(bundle)
activityInterface.onCreate(bundle);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void startActivity(Intent intent) {
String className = intent.getStringExtra("className");
Log.e("migill", "ProxyActivity startActivity " + className);
Intent proxyIntent = new Intent(this, ProxyActivity.class);
proxyIntent.putExtra("className", className);
super.startActivity(proxyIntent);
}
}
ProxyActivity中的onCreate()主要就是根據(jù)類全名實(shí)例化對象更扁,在強(qiáng)轉(zhuǎn)成ActivityInterface類型對象activityInterface盖腕,activityInterface在這個(gè)對象中注入宿主赫冬,activityInterface在執(zhí)行的onCreate()。
ProxyActivity中的startActivity()方法是重新創(chuàng)建一個(gè)ProxyActivity頁面溃列,接著就會(huì)執(zhí)行onCreate()方法劲厌。
插件activity之間實(shí)現(xiàn)跳轉(zhuǎn)的時(shí)候最終是通過調(diào)用插件中的BaseActivity中的startActivity()方法,這個(gè)方法又調(diào)用了宿主的appActivity.startActivity(intentNew)听隐,最終調(diào)用ProxyActivity中的startActivity()方法补鼻。
看到這里,我們開頭問的兩個(gè)問題就都解決了雅任,那么我們在思考一個(gè)問題风范,為什么要有代理的Activity?
那是因?yàn)椴寮械腁ctivity,并不是一個(gè)能夠運(yùn)行的組件沪么,所以需要代理的Activity去代替插件中的Activity,例如硼婿,activity的任務(wù)進(jìn)棧。