开发环境:AndroidStudio2.0,MainActivity.java源码如下:
将编译生成的out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar文件放到工程的根目录下,点击File->Project Structure,在弹出的界面中点击"+"号按钮新建Module,Module类型选择Import .JAR/.AAR Package,接下来的界面中File name选择刚才的classes.jar,最后点击Finish结束。
回到Project Structure界面中,点击左侧的app,选中右侧的Dependencies,再点击右侧的"+"号,选中"Module dependency",在弹出的界面中点击OK完成,回到Project Structure界面,选择中间的:classes,Scope选择Provided,点击OK完成。
修改配置文件:
1.app/build.gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "mobiletek.leddemo"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
+ // Enabling multidex support.
+ multiDexEnabled true
}
+ dexOptions {
+ javaMaxHeapSize "4g"
+ }
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.1.0'
compile project(':classes')
+ compile 'com.android.support:multidex:1.0.0'
}
2.修改app/src/main/AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobiletek.leddemo">
<application
+ android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package mobiletek.leddemo;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
import android.os.ILedService;
import android.os.ServiceManager;
public class MainActivity extends AppCompatActivity {
private boolean ledon = false;
private Button button = null;
private ILedService iLedService = null;
private CheckBox checkBoxLed1 = null;
private CheckBox checkBoxLed2 = null;
private CheckBox checkBoxLed3 = null;
private CheckBox checkBoxLed4 = null;
int []ledctrl = {0x12, 0x34, 0x56, 0x78};
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(ledctrl[i], 1);
} catch (Exception 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(ledctrl[i], 0);
} catch (Exception 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.LED1:
if (checked) {
Toast.makeText(getApplicationContext(), "LED1 on", Toast.LENGTH_SHORT).show();
iLedService.ledCtrl(ledctrl[0], 1);
}
else {
Toast.makeText(getApplicationContext(), "LED1 off", Toast.LENGTH_SHORT).show();
iLedService.ledCtrl(ledctrl[0], 0);
}
break;
case R.id.LED2:
if (checked) {
Toast.makeText(getApplicationContext(), "LED2 on", Toast.LENGTH_SHORT).show();
iLedService.ledCtrl(ledctrl[1], 1);
}
else {
Toast.makeText(getApplicationContext(), "LED2 off", Toast.LENGTH_SHORT).show();
iLedService.ledCtrl(ledctrl[1], 0);
}
break;
case R.id.LED3:
if (checked) {
Toast.makeText(getApplicationContext(), "LED3 on", Toast.LENGTH_SHORT).show();
iLedService.ledCtrl(ledctrl[2], 1);
}
else {
Toast.makeText(getApplicationContext(), "LED3 off", Toast.LENGTH_SHORT).show();
iLedService.ledCtrl(ledctrl[2], 0);
}
break;
case R.id.LED4:
if (checked) {
Toast.makeText(getApplicationContext(), "LED4 on", Toast.LENGTH_SHORT).show();
iLedService.ledCtrl(ledctrl[3], 1);
}
else {
Toast.makeText(getApplicationContext(), "LED4 off", Toast.LENGTH_SHORT).show();
iLedService.ledCtrl(ledctrl[3], 0);
}
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.BUTTON);
iLedService = ILedService.Stub.asInterface(ServiceManager.getService("led"));
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());
}
}
将编译生成的out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar文件放到工程的根目录下,点击File->Project Structure,在弹出的界面中点击"+"号按钮新建Module,Module类型选择Import .JAR/.AAR Package,接下来的界面中File name选择刚才的classes.jar,最后点击Finish结束。
回到Project Structure界面中,点击左侧的app,选中右侧的Dependencies,再点击右侧的"+"号,选中"Module dependency",在弹出的界面中点击OK完成,回到Project Structure界面,选择中间的:classes,Scope选择Provided,点击OK完成。
修改配置文件:
1.app/build.gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "mobiletek.leddemo"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
+ // Enabling multidex support.
+ multiDexEnabled true
}
+ dexOptions {
+ javaMaxHeapSize "4g"
+ }
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.1.0'
compile project(':classes')
+ compile 'com.android.support:multidex:1.0.0'
}
2.修改app/src/main/AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobiletek.leddemo">
<application
+ android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
本文介绍了一个简单的Android应用程序,该程序使用自定义按钮和复选框来控制LED灯的开关状态,并通过`ILedService`接口与底层硬件交互。
1146

被折叠的 条评论
为什么被折叠?



