Android 4.4.2 动态添加JNI库方法记录 (二 app应用层)
生活随笔
收集整理的這篇文章主要介紹了
Android 4.4.2 动态添加JNI库方法记录 (二 app应用层)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
歡迎轉(zhuǎn)載,務(wù)必注明出處:http://blog.csdn.net/wang_shuai_ww/article/details/44458553
源碼下載地址:http://download.csdn.net/detail/u010406724/8515377
本篇介紹怎么使用前面建立好的庫(kù)文件。
要使用JNI庫(kù)文件,那么首先我們是需要把它加載到系統(tǒng)中,并對(duì)其定義接口,供給應(yīng)用來(lái)調(diào)用。
建立一個(gè)工程,我的工程名為RealArmTest,過(guò)程就省略了,完成后再在src下建立一個(gè)類,不繼承其他類,包名為realarm.hardware,新建的類名為HardwareControl。代碼如下:
package realarm.hardware;public class HardwareControl {public native int LedSetState(int ledNum,int ledState);static {System.loadLibrary("LedJni");}}具體的工程如下圖:
主activity文件源碼如下:
package com.example.realarmtest;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.ToggleButton; import realarm.hardware.HardwareControl;;public class MainActivity extends Activity {private HardwareControl MyLedTest = null;private ToggleButton btnLed;private ImageView imageLed;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnLed = (ToggleButton) findViewById(R.id.btnLed);imageLed = (ImageView) findViewById(R.id.imageLed);MyLedTest = new HardwareControl();MyLedTest.LedSetState(0, 0);imageLed.setImageResource(R.drawable.bulboff);btnLed.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(btnLed.isChecked()){imageLed.setImageResource(R.drawable.bulbon);MyLedTest.LedSetState(0, 1);}else {imageLed.setImageResource(R.drawable.bulboff);MyLedTest.LedSetState(0, 0);}}});} }是不是發(fā)現(xiàn)更簡(jiǎn)潔了,也容易理解,O(∩_∩)O。
布局文件為:
<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="com.example.ledtest.MainActivity" ><ToggleButtonandroid:id="@+id/btnLed"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/imageLed"android:layout_alignRight="@+id/imageLed"android:layout_centerVertical="true"android:textOff="開燈"android:textOn="關(guān)燈" /><ImageViewandroid:id="@+id/imageLed"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/btnLed"android:layout_centerHorizontal="true"android:layout_marginBottom="22dp"android:src="@drawable/bulboff" /></RelativeLayout> 另外還需要資源圖片,這里就沒(méi)法貼出來(lái)了,需要的去上面提供的地址下載完整工程源碼就有了。最后愿看這些系列文章的朋友都能對(duì)Android驅(qū)動(dòng)到上層應(yīng)用有一定的了解。
總結(jié)
以上是生活随笔為你收集整理的Android 4.4.2 动态添加JNI库方法记录 (二 app应用层)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android 4.4.2 动态添加JN
- 下一篇: Fail to connect to c