1、NDK
代码重用,运行效率
NDK:Native Development Kit(工具集)(C/C++)
mk文件,so文件
libc/libm/libz/liblog
开发环境搭建:
a、NDK压缩包
b、path="NDK解压目录"
c、cmd中ndk-build(早期版本:build/host-setup.sh)
d、安装cygwin(make/gcc必装)
e、运行cygwin输入make -v/gcc -v检测是否成功
编写代码:
a、java
static{
System.loadLibrary("testNDK");
}
public native String stringTestNdk();
b、编译生成.class文件
c、javah生成.h/新建ndk文件夹在工程目录下执行:javah -classpath bin -d ndk com.blueeagle.example.TestNDK
其中-classpath表示类的路径/-d ndk表示生成.h文件的目录/com.blueeagle.TestNDK表示完整的类名
函数命名:Java_package_class_method形式命名
d、C/C++文件
#include<string.h>
#include<jni.h>
jstring Java_com_blueeagle_example_testNDK_stringTestNdk(JNIEnv* env,jobject thiz)
{
return (*env)->NewStringUTF(env, "Hello Test NDK!");
}
e、编译生成相应的库
在testNDK.c同级目录下新建一个Android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := testNDK
LOCAL_SRC_FILES := testNDK.C
include $(BUILD_SHARED_LIBRARY)
编译:工程根目录输入:ndk-build
最后:
重新编译testNDK工程
2、传感器
获取所有传感器:
SensorManager manager = (SensorManager)this.getSystemService(Context.SENSOR_SERVICE);
List<Sensor> list = manager.getSensorList(Sensor.TYPE_ALL);
使用传感器:
SensorManager manager = (SensorManager)this.getSystemService(Context.SENSOR_SERVICE);
private SensorEventListener listener = new SensorEventListener(){
public void onSensorChanged(SensorEvent event){
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
onResume()
Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
onPause()
manager.unregisterListener(listener);
super.onPause();
doSomething()
private void doLock(SensorEvent event) {
int type = event.sensor.getType();
float[] values = event.values;
float x = values[0];
float y = values[1];
float z = values[2];
if (type == Sensor.TYPE_ACCELEROMETER) {
if (x >= VALUE || x <= -VALUE || y >= VALUE || y <= -VALUE
|| z >= VALUE || z <= -VALUE) {
}
}
}
3、语音识别
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH
public void onClick_Voice_Recognizer(View view){
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "语音录入");
startActivityForResult(intent, 1);
}
onActivityResult
if(requestCode == 1){
if(resultCode == Activity.RESULT_OK){
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(matches.size() > 0){
System.out.println(matches.get(0));
}
}
}
4、GoogleMap/GPS
5、桌面插件
AppWidget
[1]、布局文件(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请叫我Widget" />
</LinearLayout>
[2]、部件配置(xml/widget_provider.xml)
<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/main"
android:minHeight="40dp"
android:minWidth="100dp"
android:updatePeriodMillis="12345" >
</appwidget-provider>
[3]、AndroidManifest.xml
<receiver
android:name=".FirstWidgetProvider"
android:label="FirstWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
[4]、来个extends AppWidgetProvider
public class FirstWidgetProvider extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
}
6、输入法开发
按类划分的字符/额外增加的字符/附加的输入规则
AndroidManifest.xml
<activity>
android:windowSoftInputMode="stateVisible|adjustPan"
编写一个输入法程序的步骤:
a、建立一个继承android.inputmethodservice.InputMethodService的类
b、AndroidManifest.xml文件中配置
c、编写一个用于显示软键盘的布局文件
d、实现InputMethodService类的onCreateInputView方法
7、测试驱动开发
黑盒测试/白盒测试/灰盒测试/迭代测试
TDD/JUnit
8、性能优化
获取时间:System.currentTimeMillis
获取内存:
long total = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().freeMemory();
long used = total - free;
TRACEVIEW:
Debug.startMethodTracing("test");
testMethod();
Debug.stopMethodTracing();