5.Android硬件访问服务使用反射

来源:互联网 发布:软件著作权申请加急 编辑:程序博客网 时间:2024/06/13 13:01

1>. 采用反射机制的方式去访问硬件

1<. 优化安装包大小

步骤:
1<. File -> Project Structure -> Modules ->

2<. app -> Dependencies -> Scope -> Provided

2<. 开始使用反射机制的改写

1<. 开始代码转换

**[!原来的]**iLedService = ILedService.Stub.asInterface(ServiceManager.getService("leds"));
**[!现在的]**Method getService = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);Object ledService = getService.invoke(null, "leds");Method asInterface = Class.forName("android.os.ILedService$Stub").getMethod("asInterface", IBinder.class);Proxy   = asInterface.invoke(null, ledService);ledCtrl = Class.forName("android.os.ILedService$Stub$Proxy").getMethod("ledCtrl", int.class, int.class);

2<. 使用:
ledCtrl.invoke(Proxy, 0, 0);

示例代码

1<. MainActivity.java –APP层

package com.becauseican.app_0001_leddemo;import android.app.Activity;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.os.ServiceManager;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.Toast;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;//import android.os.ILedService;public class MainActivity extends Activity {    private Button buttonLead = null;    private boolean boolLead = false;    private CheckBox checkBoxLed1 = null;    private CheckBox checkBoxLed2 = null;    private CheckBox checkBoxLed3 = null;    private CheckBox checkBoxLed4 = null;    //private ILedService iLedService = null;    Object Proxy = null;    Method ledCtrl = null;    /* 自己定义的类 */    class MyButtonListener implements View.OnClickListener {        @Override        public void onClick(View v) {            boolLead = !boolLead;            if (boolLead) {                buttonLead.setText("Close All");                checkBoxLed1.setChecked(true);                checkBoxLed2.setChecked(true);                checkBoxLed3.setChecked(true);                checkBoxLed4.setChecked(true);                /* 全部点亮 */                for (int i = 0; i < 4; ++i) {                    try {                        ledCtrl.invoke(Proxy, i, 1);                    } catch (IllegalAccessException e) {                        e.printStackTrace();                    } catch (InvocationTargetException e) {                        e.printStackTrace();                    }                }            }else {                buttonLead.setText("Open All");                checkBoxLed1.setChecked(false);                checkBoxLed2.setChecked(false);                checkBoxLed3.setChecked(false);                checkBoxLed4.setChecked(false);                /* 全部熄灭 */                for (int i = 0; i < 4; ++i) {                    try {                        ledCtrl.invoke(Proxy, i, 0);                    } catch (IllegalAccessException e) {                        e.printStackTrace();                    } catch (InvocationTargetException e) {                        e.printStackTrace();                    }                }            }        }    }    public void onCheckboxClicked(View view) {        // Is the view now checked?        boolean checked = ((CheckBox) view).isChecked();        // Check which checkbox was clicked        try {            switch(view.getId()) {                case R.id.CheckBoxLed4:                    if (checked) {                        Toast.makeText(getApplicationContext(), "CheckBoxLed4 on", Toast.LENGTH_SHORT).show();                        ledCtrl.invoke(Proxy, 3, 1);                    }                    // Put some meat on the sandwich                    else {                        Toast.makeText(getApplicationContext(), "CheckBoxLed4 off", Toast.LENGTH_SHORT).show();                        ledCtrl.invoke(Proxy, 3, 0);                    }                    // Remove the meat                    break;                case R.id.CheckBoxLed3:                    if (checked) {                        Toast.makeText(getApplicationContext(), "CheckBoxLed3 on", Toast.LENGTH_SHORT).show();                        ledCtrl.invoke(Proxy, 2, 1);                    }                    // Cheese me                    else {                        Toast.makeText(getApplicationContext(), "CheckBoxLed3 off", Toast.LENGTH_SHORT).show();                        ledCtrl.invoke(Proxy, 2, 0);                    }                    // I'm lactose intolerant                    break;                case R.id.CheckBoxLed2:                    if (checked) {                        Toast.makeText(getApplicationContext(), "CheckBoxLed2 on", Toast.LENGTH_SHORT).show();                        ledCtrl.invoke(Proxy, 1, 1);                    }                    // Put some meat on the sandwich                    else {                        Toast.makeText(getApplicationContext(), "CheckBoxLed2 off", Toast.LENGTH_SHORT).show();                        ledCtrl.invoke(Proxy, 1, 0);                    }                    // Remove the meat                    break;                case R.id.CheckBoxLed1:                    if (checked) {                        Toast.makeText(getApplicationContext(), "CheckBoxLed1 on", Toast.LENGTH_SHORT).show();                        ledCtrl.invoke(Proxy, 0, 1);                    }                    // Put some meat on the sandwich                    else {                        Toast.makeText(getApplicationContext(), "CheckBoxLed1 off", Toast.LENGTH_SHORT).show();                        ledCtrl.invoke(Proxy, 0, 0);                    }                    // Remove the meat                    break;                // TODO: Veggie sandwich            }        } catch (IllegalAccessException e) {            e.printStackTrace();        } catch (InvocationTargetException e) {            e.printStackTrace();        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        buttonLead = (Button) findViewById(R.id.ButtonLead);        /* 获得硬件访问服务的实例化对象 */        /* 利用反射机制实现硬件访问服务         * iLedService = ILedService.Stub.asInterface(ServiceManager.getService("leds"));         */        try {            Method getService = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);            IBinder ledService = (IBinder) getService.invoke(null, "leds");            Method asInterface = Class.forName("android.os.ILedService$Stub").getMethod("asInterface", IBinder.class);            Proxy   = asInterface.invoke(null, ledService);            ledCtrl = Class.forName("android.os.ILedService$Stub$Proxy").getMethod("ledCtrl", int.class, int.class);        } catch (NoSuchMethodException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        } catch (InvocationTargetException e) {            e.printStackTrace();        }        checkBoxLed1 = (CheckBox) findViewById(R.id.CheckBoxLed1);        checkBoxLed2 = (CheckBox) findViewById(R.id.CheckBoxLed2);        checkBoxLed3 = (CheckBox) findViewById(R.id.CheckBoxLed3);        checkBoxLed4 = (CheckBox) findViewById(R.id.CheckBoxLed4);/*      // 采用匿名类的方式        buttonLead.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v){                boolLead = !boolLead;                if (boolLead) {                    buttonLead.setText("Close All");                }else {                    buttonLead.setText("Open All");                }            }        });*/        //使用自定义类的方式        buttonLead.setOnClickListener(new MyButtonListener());    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    @Override    protected void onDestroy() {        for (int i = 0; i < 4; ++i) {            try {                ledCtrl.invoke(Proxy, i, 0);            } catch (IllegalAccessException e) {                e.printStackTrace();            } catch (InvocationTargetException e) {                e.printStackTrace();            }        }        super.onDestroy();    }}
原创粉丝点击