6.5 Android Hardware Access Service uses reflection

Source: Internet
Author: User
Tags try catch

1, the previous example of the app in order to be able to ask Iledservice interface, Import the Classes.jar into the application, but we do not want to put classes into the APK package, so that our APK program will be very large (unzip the APK will find that there are classes.dex and classes2.dex the two optimized packages)

So here's how to modify the compilation options:

In Android Studio

File->project structure-> Click on the left side of our app-> click on the right side of the dependencies to find the classes below and change the value in the scope list to provided

2. How the app uses reflection to access hardware services, as an example of a previously written app

A, remove import android.os.ILedService and import Android.os.ServiceManager, and add import Android.os.IBinder

B. New variable in class: Object proxy; Method Lec_ctrl

C, remove Iledservice =iledservice.stub.asinterface (Servicemanager.getservice ("led")); and use reflection to implement this function:

Method GetService = Class.forName ("Android.os.ServiceManager"). GetMethod ("GetService", String.class)// The first parameter of GetService is the method name obtained

Object ledservice = Getservice.invoke (null, "led")//The first parameter should be an instantiated object, but the GetService method is static, all without instantiating the object, ' Led ' is the GetService parameter, invoke is called Ledservice function, this function returns IBinder object, but invoke returns object, this ibinder will be converted to object

IBinder Ledservice = (ibinder) getservice.invoke (null, "led");//function with the above sentence

/* The function of Servicemanager.getservice ("led") has been achieved here */

Method asinterface= class.forname ("Android.os.iledservice$stub"). GetMethod ("Asinterface", ibinder.class);//Here's "$" Represents a subclass

Proxy = Asinterface.invoke (Null,ledservice); The//asinterface method returns a proxy object that is not declared in the code, all of which are received using the parent class object.

/* So far we have implemented Iledservice =iledservice.stub.asinterface (Servicemanager.getservice ("led"); function */

/* Below we will get the Led_ctrl function inside the class proxy */

Lec_ctrl= class.forname ("Android.os.iledservice$stub$proxy"). GetMethod ("Led_ctrl", Int.class,int.class);

/* Use Led_ctrl to light * *

Lec_ctrl.invoke (proxy, 0, 1);

Then we can use the proxy type object in the app Led_ctrl to operate the LED//proxy class in Iledservice.java implementation 1

D, modify catch exception, remove the previously added try...catch ... Code, after the code is selected, Ctrl+alt+t, select Try Catch

The log interface in Android Studio can also set the filter, set tag to Ledhal

6.5 Android Hardware Access Service uses reflection

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.