美文网首页Application
Android_关于 Application 实现多继承

Android_关于 Application 实现多继承

作者: 738bc070cd74 | 来源:发表于2015-07-26 16:08 被阅读2425次

    使用接口代理的方式

    • 定义一个代理的 ProxyApplication 继承 Application

        public class ProxyApplication extends Application {    
             private IApplicationListener listener;    
             @Override    
             public void onCreate() {        
             super.onCreate();        
             if(listener != null){            
                 listener.onProxyCreate();        
             }    
        }    
        @Override    
        public void attachBaseContext(Context base){            
            super.attachBaseContext(base);       
            this.listener = initProxyApplication();        
            if(this.listener != null){               
            this.listener.onProxyAttachBaseContext(base);        
            }   
        }    
        @Override    
        public void onConfigurationChanged(Configuration newConfig) {   
            super.onConfigurationChanged(newConfig);        
            if(this.listener != null){              
                 this.listener.onProxyConfigurationChanged(newConfig);   
        }    
        }    
        private IApplicationListener initProxyApplication() {        
        try {            
            Class clazz = Class.forName("speed.huben.testmultiextendsapplication.ImplementApplication");           
            return (IApplicationListener)clazz.newInstance();       
        } catch (ClassNotFoundException e) {            
            e.printStackTrace();        
        } catch (InstantiationException e) {            
            e.printStackTrace();       
        } catch (IllegalAccessException e) {            
            e.printStackTrace();        }        
        return null;    }}
      
    • 自定义HApplication 继承 ProxyApplication

      <application    
           android:name=".HApplication"
           ...
      
    • 定义一个IApplicationListener

      public interface IApplicationListener {    
           public void onProxyCreate();    
           public void onProxyAttachBaseContext(Context base);    
           public void onProxyConfigurationChanged(Configuration newConfig);
      }
      
    • 具体实现对应的 Application 实现 IApplicationListener

        public class BDProxyApplication extends BaiduApplication implements IApplicationListener{
      
        @Override
        public void onProxyCreate() {
        super.onCreate();
        }
      
        @Override
        public void onProxyAttachBaseContext(Context base) {
        super.attachBaseContext(base);
        }
      
       @Override
       public void onProxyConfigurationChanged(Configuration config) {
       super.onConfigurationChanged(config);
       }
       }
      

    使用内部类

    • 代理实现的ProxyApplication

       public class ProxyApplication extends Application {    
       @Override    
       public void onCreate() {        
       super.onCreate();        
       new ImplBaiduApplication().onCreate();        
       new ImplTencentApplication().onCreate();   
       }    
       @Override    
       public void attachBaseContext(Context base){              
       super.attachBaseContext(base);    }    
       @Override    
       public void onConfigurationChanged(Configuration newConfig) {        
       super.onConfigurationChanged(newConfig);    }    
      
       public class ImplBaiduApplication extends BaiduApplication { 
       @Override        
       public void onCreate() {            
       super.onCreate();        }    }    
       public class ImplTencentApplication extends TencentApplication {        
       @Override       
       public void onCreate() {            
       super.onCreate();        }    }}
      

    demo实现

    https://github.com/huben/TestMultiExtendsApplication

    相关文章

      网友评论

        本文标题:Android_关于 Application 实现多继承

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