Android RoboGuice User Guide (16): Standard Injection

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.