Android Drive Learning-app two ways to operate new hardware (support for added drivers)

Source: Internet
Author: User

After adding a new driver to Android, how does the app work?

Normal use of a device requires getservice. But like LEDs and other hardware drivers we add ourselves, the Android source code doesn't have the services we add ourselves.

The first type:

Our own created hardware device-driven classes are defined by the system in order to hide classes, so how do you use hidden classes in an Android system? For this we can see that the classes we added are added to Framework.jar based on the Android build process. But Framework.jar is the DEX format and uses the original jar target file when we do the app.

So we use Out/target/common/obj/java_libraries/framework_intermediates/class.jar this jar file.

How to include jar files, we can Google.

The first step is to put the jar package in the Libs directory under the project, and if not, create a new directory.

Second, through the File menu or F4 into project structure, first select the + sign in the upper left, then add Class.jar, then select the app on the left, then go to dependencies, click + Select File Dependency, Select the jar package you want from the Libs directory, and then set the scope to provide. Can.The second type:Create our new class with reflection. That is, you can create and function calls to our custom classes without importing the Class.jar file. My personal understanding is to use the Class.forName static function to implement a call to a function by a known function name.

Class.forName: Returns a class object that is associated with the given string name or interface .

Class.forName is a static method that can also be used to load classes. There are two forms of this method: Class.forName (String name, Boolean initialize, ClassLoader loader), and Class.forName (string className). The first form of the parameter name represents the full name of the class, initialize indicates whether the class is initialized, and loader represents the class loader used when loading. The second form is equivalent to setting the value of the parameter initialize to True,loader as the class loader of the current class.

static   Class <?>

forname ( String   className)

Returns the  Class  object associated with the class or interface with the given string name.

static   Class <?>

forname ( String   name, boolean initialize,   ClassLoader   loader)

Returns the  Class  object associated with the class or interface with the given string name, using the given class loader.

Gets the class by the class name.

Class ServiceManager = Class.forName ("Android.os.ServiceManager");

Get method

method = Servicemanager.getmethod ("GetService", String.class);

Calling methods

Method.invoke (Servicemanager.newinstance (), "phone");

() http://blog.csdn.net/fengyuzhengfan/article/details/38086743

Android Drive Learning-app two ways to operate new hardware (support for added drivers)

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.