Welcome reprint, be sure to indicate the source: http://blog.csdn.net/wang_shuai_ww/article/details/44458553
Source: http://download.csdn.net/detail/u010406724/8515377
This article describes how to use the previously established library files.
To use the JNI library file, first we need to load it into the system and define the interface for it to be called by the application.
Build a project, my project named Realarmtest, the process is omitted, after completion of a class under SRC, do not inherit other classes, package named Realarm.hardware, the new class name is Hardwarecontrol. The code is as follows:
Package Realarm.hardware;public class Hardwarecontrol {public native int ledsetstate (int lednum,int ledstate); static { System.loadlibrary ("Ledjni");}}
Specific works such as:
The main activity file source code is as follows:
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 I Mageview imageled; @Override protected 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);}}); }}
is not found more concise, also easy to understand, O (∩_∩) O.
The layout file is:
<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:paddi ngbottom= "@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" > <togglebutton android:id= "@+id/btnled" Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignleft= "@+id/imageLed" an droid:layout_alignright= "@+id/imageled" android:layout_centervertical= "true" android:textoff= "turn on the Light" and Roid:texton= "Off light"/> <imageview android: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>
Also need a resource picture, here can not be posted out, need to go to the address provided above to download the complete project source code is there.
Finally, I would like to see the Friends of these series of articles can have a certain understanding of the Android drive to the upper application.
Android 4.4.2 Dynamic Add Jni Library method record (two app tiers)