占位式插件化框架—Activity通信
占位式插件化框架—Service通信
占位式插件化框架—动态广播的使用
我们要实现插件里面的静态广播,那么我们就要在宿主中获取插件中AndroidManifest.xml里面配置的静态接收者。
静态注册的广播,是什么时候注册的?
手机开机的时候,所有的app,在进行安装一遍,系统会去解析AndroidManifest,解析静态广播后,就会去注册。
系统是如何去解析apk文件里面的组件信息的?
我们看一下pms如何处理data/app目录,解析apk的
scanDirTracedLI 是要去扫描 /data/app/目录下的apk文件 解析AndroidManifest里面的所有信息
一步步的跟下去,会调用PackageParser.parsePackage()获取一个Package对象
parsePackage是解析 apk 文件里面的所有信息
Package是apk里面的AndroidManifest配置信息 (所有的)
这样拿到了Package,就能拿到静态的广播信息
在宿主中注册广播context.registerReceiver(broadcastReceiver, intentFilter),那么我们就需要IntentFilter和广播接收者的名字,我们解析了p.apk,拿到Package对象,怎么获取它们呢?
看到Activity继承Component,在跟进去看IntentInfo extends IntentFilter ,那ArrayList<II> intents就是我们要找的IntentFilter。
我们看到在Activity类中有ActivityInfo info属性,ActivityInfo extends ComponentInfo,ComponentInfo extends PackageItemInfo,在PackageItemInfo中有String name属性,这个就是组件的名字。
在PackageParser类中找到,通过这个方法ActivityInfo generateActivityInfo(Activity a, int flags,
PackageUserState state, int userId)就可以获取ActivityInfo,从而获取控件的名字。
public class StaticReceiver extends BroadcastReceiver {
private final String SACTION_1 = "plugin.static_receiver_1";
private final String SACTION_2 = "plugin.static_receiver_2";
private final String SACTION_3 = "plugin.static_receiver_3";
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "我是静态广播StaticReceiver,我接收到了广播", Toast.LENGTH_LONG).show();
if (intent.getAction().equals(SACTION_1)) {
Log.e("migill", "我是静态广播StaticReceiver,我接收到了广播 action:" + SACTION_1);
}
if (intent.getAction().equals(SACTION_2)) {
Log.e("migill", "我是静态广播StaticReceiver,我接收到了广播 action:" + SACTION_2);
}
if (intent.getAction().equals(SACTION_3)) {
Log.e("migill", "我是静态广播StaticReceiver,我接收到了广播 action:" + SACTION_3);
}
}
}
配置AndroidManifest.xml
<receiver android:name=".StaticReceiver">
<intent-filter>
<action android:name="plugin.static_receiver_1" />
<action android:name="plugin.static_receiver_2" />
</intent-filter>
<intent-filter>
<action android:name="plugin.static_receiver_3" />
</intent-filter>
</receiver>
在PluginActivity中增加三个发送广播的按钮
public class PluginActivity extends BaseActivity {
private final String ACTION = "com.migill.plugin_package.ACTION";
private final String SACTION_1 = "plugin.static_receiver_1";
private final String SACTION_2 = "plugin.static_receiver_2";
private final String SACTION_3 = "plugin.static_receiver_3";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plugin);//执行的是BaseActivity中的setContentView方法
Log.e("migill", "我是插件PluginActivity");
findViewById(R.id.bt_send_sreceiver1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(SACTION_1);
sendBroadcast(intent);
}
});
findViewById(R.id.bt_send_sreceiver2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(SACTION_2);
sendBroadcast(intent);
}
});
findViewById(R.id.bt_send_sreceiver3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(SACTION_3);
sendBroadcast(intent);
}
});
}
}
编译apk,重命名为p.apk,并放入 /storage/emulated/0/Android/data/com.migill.pluginproject/files/这个目录下。
在PluginManager类中加入parserApkAction()方法,获取p.apk中AndroidManifest.xml静态注册的广播的类名和intent-filter。
public class PluginManager {
......
public void parserApkAction() {
try {
//插件包路径
File file = new File(context.getExternalFilesDir(null).getAbsolutePath() + File.separator + "p.apk");
if (!file.exists()) {
Log.e("migill", "插件包 不存在...");
return;
}
String pluginPath = file.getAbsolutePath();
Log.e("migill", "pluginPath : " + pluginPath);
//实例化 PackageParser对象
Class mPackageParserClass = Class.forName("android.content.pm.PackageParser");
Object mPackageParser = mPackageParserClass.newInstance();
// 1.执行此方法 public Package parsePackage(File packageFile, int flags),就是为了,拿到Package
Method mPackageParserMethod = mPackageParserClass.getMethod("parsePackage", File.class, int.class);
Object mPackage = mPackageParserMethod.invoke(mPackageParser, file, PackageManager.GET_ACTIVITIES);
//获取mPackage中的ArrayList<Activity> receivers属性
Field receiversField = mPackage.getClass().getDeclaredField("receivers");
Object receivers = receiversField.get(mPackage);
ArrayList arrayList = (ArrayList) receivers;
//此Activity不是组件的Activity,是PackageParser里面的内部类
for (Object mActivity : arrayList) {// mActivity --> <receiver android:name=".StaticReceiver">
//通过反射拿到intents ArrayList<II> intents; 一个<receiver>标签可以对应多个Intent-Filter
Class mComponentClass = Class.forName("android.content.pm.PackageParser$Component");
Field intentsField = mComponentClass.getDeclaredField("intents");
ArrayList<IntentFilter> intents = (ArrayList) intentsField.get(mActivity);
//上面是拿到了IntentFilter,下面就是获取组件的名字 activityInfo.name
/**
* 执行此方法,就能拿到 ActivityInfo
* public static final ActivityInfo generateActivityInfo(Activity a, int flags,
* PackageUserState state, int userId)
*/
Class mPackageUserState = Class.forName("android.content.pm.PackageUserState");
Class mUserHandle = Class.forName("android.os.UserHandle");
int userId = (int) mUserHandle.getMethod("getCallingUserId").invoke(null);
Method generateActivityInfoMethod = mPackageParserClass.getDeclaredMethod("generateActivityInfo",
mActivity.getClass(), int.class, mPackageUserState, int.class);
generateActivityInfoMethod.setAccessible(true);
ActivityInfo mActivityInfo = (ActivityInfo) generateActivityInfoMethod.invoke(null, mActivity, 0, mPackageUserState.newInstance(), userId);
String receiverClassName = mActivityInfo.name;
Log.e("migill", "receiverClassName : " + receiverClassName);
Class mStaticReceiverClass = getClassLoader().loadClass(receiverClassName);
BroadcastReceiver broadcastReceiver = (BroadcastReceiver) mStaticReceiverClass.newInstance();
for (IntentFilter intentFilter : intents) {
Log.e("migill","intentFilter mActions size " + intentFilter.countActions());
context.registerReceiver(broadcastReceiver, intentFilter);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
.....
//注册 插件里面 配置的 静态广播接收者
public void loadStaticReceiver(View view) {
PluginManager.getInstance(this).parserApkAction();
}
//发送广播
public void sendStaticReceiver(View view) {
Intent intent = new Intent();
intent.setAction("plugin.static_receiver_1");
sendBroadcast(intent);
}
}