Android的应用开发
这一部分我们粗糙的讲一下,就是弄几个按键,然后调用刚才生成的类,然后下载到smart210上做一个测试。
1.更新LEDAPP下面的res/layout/activity_main.xml,这是Android应用程序的界面布局文件,具体的更新内容如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/led1_on"
android:text="@string/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
/>
<Button
android:id="@+id/led1_off"
android:text="@string/myButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="115dip"
/>
<Button
android:id="@+id/led2_on"
android:text="@string/myButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="245dip"
/>
<Button
android:id="@+id/led2_off"
android:text="@string/myButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="355dip"
/>
<Button
android:id="@+id/led3_on"
android:text="@string/myButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dip"
android:layout_marginLeft="5dip"
/>
<Button
android:id="@+id/led3_off"
android:text="@string/myButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dip"
android:layout_marginLeft="115dip"
/>
<Button
android:id="@+id/led4_on"
android:text="@string/myButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dip"
android:layout_marginLeft="245dip"
/>
<Button
android:id="@+id/led4_off"
android:text="@string/myButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dip"
android:layout_marginLeft="355dip"
/>
</RelativeLayout>
2.更新LEDAPP下的res/values/strings.xml的内容,这个里面的内容在前面的activity_main.xml的布局文件中需要,具体内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string>LEDAPP</string>
<string>Settings</string>
<string>Hello world!</string>
<string>LED1_ON</string>
<string>LED1_OFF</string>
<string>LED2_ON</string>
<string>LED2_OFF</string>
<string>LED3_ON</string>
<string>LED3_OFF</string>
<string>LED4_ON</string>
<string>LED4_OFF</string>
</resources>
3.现在看看MainActivity.java文件,在他里面的具体调用刚生成的类,具体的代码如下:
package com.ndk.led;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
static {
System.loadLibrary("LEDAPP");
}//上面的System.loadLibrary("LEDAPP")这一句一定要加上,这是调用刚才生成的库的函数,负责会出现“unfortunately,xxx has stopped!”的错误
Button Button1,Button2,Button3,Button4,Button5,Button6,Button7,Button8;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JniLed.LedInit();
Button1 = (Button)findViewById(R.id.led1_on);
Button2 = (Button)findViewById(R.id.led1_off);
Button3 = (Button)findViewById(R.id.led2_on);
Button4 = (Button)findViewById(R.id.led2_off);
Button5 = (Button)findViewById(R.id.led3_on);
Button6 = (Button)findViewById(R.id.led3_off);
Button7 = (Button)findViewById(R.id.led4_on);
Button8 = (Button)findViewById(R.id.led4_off);
class ButtonClick implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.led1_on:
//Toast.makeText(MainActivity.this, "按钮1", Toast.LENGTH_LONG).show();
JniLed.LedIOCTL(1,0);
break;
case R.id.led1_off:
JniLed.LedIOCTL(0,0);
break;
case R.id.led2_on:
JniLed.LedIOCTL(1,1);
break;
case R.id.led2_off:
JniLed.LedIOCTL(0,1);
break;
case R.id.led3_on:
JniLed.LedIOCTL(1,2);
break;
case R.id.led3_off:
JniLed.LedIOCTL(0,2);
break;
case R.id.led4_on:
JniLed.LedIOCTL(1,3);
break;
case R.id.led4_off:
JniLed.LedIOCTL(0,3);
break;
}
}
}
Button1.setOnClickListener(new ButtonClick());
Button2.setOnClickListener(new ButtonClick());
Button3.setOnClickListener(new ButtonClick());
Button4.setOnClickListener(new ButtonClick());
Button5.setOnClickListener(new ButtonClick());
Button6.setOnClickListener(new ButtonClick());
Button7.setOnClickListener(new ButtonClick());
Button8.setOnClickListener(new ButtonClick());
}
}
这里再简单第说一下引起“unfortunately,xxxhas stopped!”的错误,一般是函数的名字不一致,没有加载创建的库所引起的。好吧,就这么多吧,希望对大家有用,下面是Android下的源代码以及smart210的led驱动文件,链接如下:
下面是在smart210上截取效果图片: