其实上层应用篇 很简单
对于BeepActivity.java 可能需要注意一下的就是 包名、类名、方法名的编写一定要与JNI层定义的方法名要一致 不然会提示找不到JNI层的方法的
比如 包名com.under.beep 类名BeepActivity 方法名beepOn
BeepActivity.java
package com.under.beep; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.view.Menu; import android.view.View; import android.widget.CheckBox; public class BeepActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_beep); if(!beepInit()) //蜂鸣器初始化函数 { new AlertDialog.Builder(BeepActivity.this).setTitle("error").setMessage("failed to init beep.").setPositiveButton("确定", null).show(); } } public void checkOnOff(View view) { switch (view.getId()) { case R.id.checkBox1: //蜂鸣器的单击事件 CheckBox checkBox=(CheckBox)view; if(checkBox.isChecked()) beepOn(1); else beepOff(0); break; case R.id.button1: //退出系统 this.finish(); break; } } //本地函数 public native boolean beepOn(int cmd); public native boolean beepOff(int cmd); public native boolean beepInit(); public native boolean beepClose(); //加载动态共享库 static{ System.loadLibrary("beepunder"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.beep, menu); return true; } }
布局文件
activity_beep.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" 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=".BeepActivity" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="checkOnOff" android:text="打开蜂鸣器" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="checkOnOff" android:text="退出" /> </LinearLayout>