For convenience, Roboguice provides "Standard injection" support for common Android objects or services. For example, you do not need to use (SensorManager) getSystemService (SENSOR_SERVICE) to obtain the SensorManger instance, but directly use the @ Inject mark.
@ Inject SensorManager sensorManager;
Roboguice automatically injects the required SensorManger object to the sensorManager. The standard injection supported by Roboguice is as follows:
@ Inject ContentResolver contentResolver;
@ Inject AssetManager assetManager;
@ Inject Resources resources;
@ Inject LocationManager locationManager;
@ Inject WindowManager windowManager;
@ Inject LayoutInflater layoutInflater;
@ Inject ActivityManager activityManager;
@ Inject PowerManager powerManager;
@ Inject AlarmManager alarmManager;
@ Inject icationicationmanager NotificationManager;
@ Inject KeyguardManager keyguardManager;
@ Inject SearchManager searchManager;
@ Inject Vibrator vibrator;
@ Inject ConnectivityManager connectivityManager;
@ Inject WifiManager wifiManager;
@ Inject InputMethodManager inputMethodManager;
@ Inject SensorManager sensorManager;
The SensorManager is used as an example to describe the usage of Standard Injection. This example is based on the Android ApiDemos example resolution (90): OS-> Sensors.
Modify
Private SensorManager mSensorManager;
Is
Private @ Inject
SensorManager mSensorManager;
Remove
MSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE );
Run the instance. You can see that getSystemService is not used. Roboguice automatically assigned a value for mSensorManager.
Download this example: http://www.bkjia.com/uploadfile/2012/0507/20120507110451171.zip
Excerpted from the mobile app