学完安卓驱动硬件访问流程之后,我们使用安卓的硬件访问服务来访问LED,所以本文是基于LED初体验编写的。
一、从上节硬件访问服务框架我们知道,安卓系统提供并实现ILedService.java函数供APP调用,所以我们就可以利用iLedService来实现对LED的控制。在代码中原来我们直接利用JNI的部分就可以省略掉,包括:打开、关闭、控制,进而替换为服务的注册,服务函数的调用等。
代码:
package com.thisway.app_0001_leddemo;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
<span style="color:#ff0000;">import android.os.ILedService;
import android.os.ServiceManager;
</span>
public class MainActivity extends AppCompatActivity {
private boolean ledon = false;
private Button button = null;
private CheckBox checkBoxLed1 = null;
private CheckBox checkBoxLed2 = null;
private CheckBox checkBoxLed3 = null;
private CheckBox checkBoxLed4 = null;
private ILedService iLedService = null;
class MyButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
ledon = !ledon;
if (ledon) {
button.setText("ALL OFF");
checkBoxLed1.setChecked(true);
checkBoxLed2.setChecked(true);
checkBoxLed3.setChecked(true);
checkBoxLed4.setChecked(true);
try {
for (int i = 0; i < 4; i++)
iLedService.ledCtrl(i, 1);
} catch (RemoteException e) {
e.printStackTrace();
}
}
else {
button.setText("ALL ON");
checkBoxLed1.setChecked(false);
checkBoxLed2.setChecked(false);
checkBoxLed3.setChecked(false);
checkBoxLed4.setChecked(false);
try {
for (int i = 0; i < 4; i++)
iLedService.ledCtrl(i, 0);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
try {
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.LED1:
if (checked) {
// Put some meat on the sandwich
Toast.makeText(getApplicationContext(), "LED1 on", Toast.LENGTH_SHORT).show();
<span style="color:#ff0000;">iLedService.ledCtrl(0, 1);</span>
}
else {
// Remove the meat
Toast.makeText(getApplicationContext(), "LED1 off", Toast.LENGTH_SHORT).show();
<span style="color:#ff0000;">iLedService.ledCtrl(0, 0);</span>
}
break;
case R.id.LED2:
if (checked) {
// Put some meat on the sandwich
Toast.makeText(getApplicationContext(), "LED2 on", Toast.LENGTH_SHORT).show();
<span style="color:#ff0000;">iLedService.ledCtrl(1, 1);</span>
}
else {
// Remove the meat
Toast.makeText(getApplicationContext(), "LED2 off", Toast.LENGTH_SHORT).show();
<span style="color:#ff0000;">iLedService.ledCtrl(1, 0);</span>
}
break;
case R.id.LED3:
if (checked) {
// Put some meat on the sandwich
Toast.makeText(getApplicationContext(), "LED3 on", Toast.LENGTH_SHORT).show();
<span style="color:#ff0000;">iLedService.ledCtrl(2, 1);</span>
}
else {
// Remove the meat
Toast.makeText(getApplicationContext(), "LED3 off", Toast.LENGTH_SHORT).show();
<span style="color:#ff0000;">iLedService.ledCtrl(2, 0);</span>
}
break;
case R.id.LED4:
if (checked) {
// Put some meat on the sandwich
Toast.makeText(getApplicationContext(), "LED4 on", Toast.LENGTH_SHORT).show();
<span style="color:#ff0000;">iLedService.ledCtrl(3, 1);</span>
}
else {
// Remove the meat
Toast.makeText(getApplicationContext(), "LED4 off", Toast.LENGTH_SHORT).show();
<span style="color:#ff0000;">iLedService.ledCtrl(3, 0);</span>
}
break;
// TODO: Veggie sandwich
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.BUTTON);
<span style="color:#ff0000;"> iLedService = ILedService.Stub.asInterface(ServiceManager.getService("led"));</span>
checkBoxLed1 = (CheckBox) findViewById(R.id.LED1);
checkBoxLed2 = (CheckBox) findViewById(R.id.LED2);
checkBoxLed3 = (CheckBox) findViewById(R.id.LED3);
checkBoxLed4 = (CheckBox) findViewById(R.id.LED4);
button.setOnClickListener(new MyButtonListener());
/*
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
ledon = !ledon;
if (ledon)
button.setText("ALL OFF");
else
button.setText("ALL ON");
}
});
*/
}
@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);
}
}
二、在编译APK过程中遇到的问题
(1)、如何与隐藏或者内部的API建立联系
首先应拷贝出jar包比如这次的classes jar包,路径:
out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar
包含之android studio的方法为: