Android 依赖注入框架RoboGuice
概述
- 控件注入:用@InjectViews方法初始化控件,例如:@InjectView(R.id.textview1)TextView textView1。
- 资源注入:用@InjectResources方法初始化资源,例如:@InjectResource(R.string.app_name)String name。
- 系统服务注入:用@Inject方法初始化并获取系统服务,例如:@Inject LayoutInflater inflater。
- POJO对象注入:用@Inject方法注入并初始化POJO对象,例如:@Inject Foo foo。
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); name = (TextView) findViewById(R.id.name); thumbnail = (ImageView) findViewById(R.id.thumbnail); loc = (LocationManager) getSystemService(Activity.LOCATION_SERVICE); icon = getResources().getDrawable(R.drawable.icon); myName = getString(R.string.app_name); name.setText( "Hello, " + myName ); } }使用RoboGuice:
@ContentView(R.layout.main) class RoboWay extends RoboActivity { @InjectView(R.id.name) TextView name; @InjectView(R.id.thumbnail) ImageView thumbnail; @InjectResource(R.drawable.icon) Drawable icon; @InjectResource(R.string.app_name) String myName; @Inject LocationManager loc; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); name.setText( "Hello, " + myName ); } }
使用前准备工作
安装
方式一:添加依赖库:(不推荐,在androidStudio上报错,很诡异。不知道为什么)
- RoboGuice 3.0
- RoboBlender 3.0
- jsr330 javax.inject (JSR-330 support)
- jsr305 (optional support for @Nullable annotation, if desired)
dependencies { provided 'org.roboguice:roboblender:3.0' compile 'org.roboguice:roboguice:3.0' }
继承类:
- RoboActivity
- RoboListActivity
- RoboExpandableListActivity
- RoboMapActivity
- RoboPreferenceActivity
- RoboAccountAuthenticatorActivity
- RoboActivityGroup
- RoboTabActivity
- RoboFragmentActivity
- RoboLauncherActivity
- RoboService
- RoboIntentService
- RoboFragment
- RoboListFragment
- RoboDialogFragment
- etc.
Injection
- Inherit from RoboActivity
- Set your content view
- Annotate your views with @InjectView
public class MyActivity extends RoboActivity { @InjectView(R.id.text1) TextView textView; @Override protected void onCreate( Bundle savedState ) { setContentView(R.layout.myactivity_layout); textView.setText("Hello!"); } }@ContentView注解:
@ContentView(R.layout.myactivity_layout) public class MyActivity extends RoboActivity { @InjectView(R.id.text1) TextView textView; @Override protected void onCreate( Bundle savedState ) { textView.setText("Hello!"); } }
将View注入Fragment@InjectView
@InjectView TextView commentEditText; @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); commentEditText.setText("Some comment"); }
注入Resource到Activity:@InjectResource
public class MyActivity extends RoboActivity { @InjectResource(R.anim.my_animation) Animation myAnimation; }
注入System Service:@Inject
class MyActivity extends RoboActivity { @Inject Vibrator vibrator; @Inject NotificationManager notificationManager;
注入POJO:@Inject
class MyActivity extends RoboActivity { @Inject Foo foo; // this will basically call new Foo(); }当然如果不想使用默认构造器,可以注解构造器:
class Foo { Bar bar; @Inject public Foo(Bar bar) { this.bar = bar; } }也可以注解POJO的Field:
class Foo { @Inject Bar bar; // Roboguice doesn't need a constructor, but you might... }
注入Singleton
前提:对单例类使用@Singleton@Singleton //a single instance of Foo is now used though the whole app class Foo { }
class MyActivity extends RoboActivity { @Inject Foo foo; // this will basically call new Foo(); }注意:这个单例一旦创建会一直驻留内存,其他类似的还有@ContextSingleton,@FragmentSingletion,他们的作用范围比较小,利于垃圾回收。
自定义Binding
public interface IFoo {} public class Foo implements IFoo {}
public class MyActivity extends RoboActivity { //How to tell RoboGuice to inject an instance of Foo ? @Inject IFoo foo; }
<application ...> <meta-data android:name="roboguice.modules" android:value="com.example.MyModule,com.example.MyModule2" /> </application>2.继承AbstractModule
public class MyModule extends AbstractModule { //a default constructor is fine for a Module public void bind() { bind(IFoo.class).to(Foo.class); } }
public class MyModule2 extends AbstractModule { //if your module requires a context, add a constructor that will be passed a context. private Context context; //with RoboGuice 3.0, the constructor for AbstractModule will use an `Application`, not a `Context` public MyModule( Context context ) { this.context = context; } public void bind() { bind(IFoo.class).toInstance( new Foo(context)); } }
注入到Service与BroadcastReceiver:
public class MyService extends RoboService { @Inject ComputeFooModule computeFooModule;
public class MyBroadcastReceiver extends RoboBroadcastReceiver { @Inject ComputeFooModule computeFooModule;
注入到自定义View
class MyView extends View { @Inject Foo foo; @InjectView(R.id.my_view) TextView myView; public MyView(Context context) { inflate(context,R.layout.my_layout, this); RoboGuice.getInjector(getContext()).injectMembers(this);//注意这一行,为了使其正常工作。 } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); inflate(context,R.layout.my_layout, this); RoboGuice.getInjector(getContext()).injectMembers(this); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); inflate(context,R.layout.my_layout, this); RoboGuice.getInjector(getContext()).injectMembers(this); } @Override public void onFinishInflate() { super.onFinishInflate(); //All injections are available from here //in both cases of XML and programmatic creations (see below) myView.setText(foo.computeFoo()); } }
使用RoboGuice Event
public class MyActivity extends RoboActivity { // Any method with void return type and a single parameter with @Observes annotation // can be used as an event listener. This one listens to onResume. public void doSomethingOnResume( @Observes OnResumeEvent onResume ) { Ln.d("Called doSomethingOnResume in onResume"); } // As you might expect, some events can have parameters. The OnCreate event // has the savedInstanceState parameter that Android passes to onCreate(Bundle) public void doSomethingElseOnCreate( @Observes OnCreateEvent onCreate ) { Ln.d("onCreate savedInstanceState is %s", onCreate.getSavedInstanceState()) } // And of course, you can have multiple listeners for a given event. // Note that ordering of listener execution is indeterminate! public void xxx( @Observes OnCreateEvent onCreate ) { Ln.d("Hello, world!") } }
本文还有很多特性没有介绍到,后续可能会进行补充。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。