你好,游客 登录 注册 搜索
背景:
阅读新闻

Android应用程序通过JNI调用驱动程序(友善Smart210)

[日期:2014-04-05] 来源:Linux社区  作者:xie0812 [字体:大 中 小]

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 name="app_name">LEDAPP</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
   
    <string name="myButton1">LED1_ON</string>
    <string name="myButton2">LED1_OFF</string>
    <string name="myButton3">LED2_ON</string>
    <string name="myButton4">LED2_OFF</string>
    <string name="myButton5">LED3_ON</string>
    <string name="myButton6">LED3_OFF</string>
    <string name="myButton7">LED4_ON</string>
    <string name="myButton8">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上截取效果图片:

 

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
相关资讯       Android JNI调用 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* canfyi会员 发表于 2014/4/11 16:12:36
厉害!