TQ210搭载Android4.0.3系统构建之BEEP从驱动到HAL到JNI到应用程序(上层应用篇)

  其实上层应用篇 很简单

  对于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>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值