美文网首页Android 插件式Android
Android-插件化三插桩实现广播的加载

Android-插件化三插桩实现广播的加载

作者: 何以邀微微 | 来源:发表于2022-03-10 15:00 被阅读0次

    本篇是在实现Activity的基础上做的,插件apk的加载这块没有变化,所以本篇之介绍关于Broadcast加载的内容

    1.宿主app

    public class ProxyBroadCast extends BroadcastReceiver {
    
        private String mClassName;
        private MSInterfaceBroadcast msInterfaceBroadcast;
    
        public ProxyBroadCast(String className,Context context) {
            this.mClassName = className;
            try {
                Class<?> aClass = PluginLoadManager.getInstance().getDexClassLoader().loadClass(mClassName);  //记载插件广播类文件
                Constructor<?> constructor = aClass.getConstructor(new Class[]{});  //得到构造函数
                Object o = constructor.newInstance();                               //实例化函数
                if (o instanceof MSInterfaceBroadcast){
                    msInterfaceBroadcast= (MSInterfaceBroadcast) o;
                    msInterfaceBroadcast.attach(context);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            msInterfaceBroadcast.onReceive(context,intent); //转发到插件广播接收者
        }
    }
    
    

    这部分是广播的插桩部分的实现,主要包含两部分功能:

    • 1.加载插件广播类并实例化,注入上下文
    • 2.接收到广播转发到插件广播中去

    2.广播接口标准

    public interface MSInterfaceBroadcast {
    
        public void attach(Context context);
    
        public void onReceive(Context context, Intent intent);
    
    
    }
    

    这部分是根据BroadcastReceiver来定义的,可以根据需求进行增减

    3.广播插件部分的实现

    public class PluginReceiver extends BroadcastReceiver implements MSInterfaceBroadcast {
    
        @Override
        public void attach(Context context) {
            Toast.makeText(context, "attach success", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "插件收到广播", Toast.LENGTH_SHORT).show();
        }
    }
    

    插件部分定义好接收者,并弹出了Toast

    public class MainActivity extends BaseActivity {
    
        static final String ACTION = "com.meishe.PLUGIN_ACTION";
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            View viewById = findViewById(R.id.image);
    
            IntentFilter intentFilter=new IntentFilter();
            intentFilter.addAction(ACTION);
            registerReceiver(new PluginReceiver(),intentFilter);
    
            viewById.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(that,"发送广播",Toast.LENGTH_SHORT).show();
                    startService(new Intent(that,PluginService.class));
                  sendBroadcast(new Intent(ACTION));
                }
            });
        }
    }
    

    为了简化,直接在插件的主页面做了广播的注册,并通过点击事件发送广播

    注意:sendBroadcast,registerReceiver 这两个方法不能调用系统方法,需要重写

    class BaseActivity extends AppCompatActivity implements MSInterfaceActivity {
    
        protected  Activity that;
    
        @Override
        public void attach(Activity proxyActivity) {
            that =proxyActivity;
        }
        
        ...
            
        @Override
        public void sendBroadcast(Intent intent) {
            that.sendBroadcast(intent);
        }
    
    
        @Override
        public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
            return that.registerReceiver(receiver, filter);
        }
    ...
       
    }
    

    这是发送广播和注册广播重写的部分代码逻辑,只是做了一个转发

    测试能够完美运行……

    相关文章

      网友评论

        本文标题:Android-插件化三插桩实现广播的加载

        本文链接:https://www.haomeiwen.com/subject/miwfdrtx.html