Android之开发杂记(一)

1、cygwin环境变量设置

可在Cygwin.bat 中设置

set NDK_ROOT=P:/android/android-ndk-r8e

或者在homeAdministrator.bash_profile中设置

NDK_ROOT=/cygdrive/p/android/android-ndk-r8e
export NDK_ROOT

或者在运行程序前设置(绿色方式)

setlocal enabledelayedexpansion
set NDK_ROOT=%cd%android-ndk-r8e
start %cd%adt-bundle-windows-x86-20130522eclipseeclipse.exe

NDK与eclipse在同一级目录下。

2、Android 属性相关

<application android:icon="@drawable/icon"//应用安装后桌面显示的图标

android:label="@string/app_name">

<activity android:name=".FormulaStudy" android:theme="@android:style/Theme.NoTitleBar" //无标题
android:screenOrientation="sensorLandscape" //只允许横屏切换
android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> //第一个Activity
</intent-filter>
</activity>
<uses-library android:name="com.noahedu"/>

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="10"/>
<uses-permission android:name="com.noahedu.permission.AWARD_SCORE"/>

</application>

3、Shell多行注释

: :||:<<COMMENTS

注释内容

COMMENTS

4、SourceInsight配置

AStyle格式化工具: 在Command增加AStyle,在Run填写"~AStyle.exe" --style=linux -s4 -S -N -L -m0 -M40 --suffix=none --convert-tabs %f,再配置快捷键

TabSiPlus外挂式的文件标签,下载后运行后再执行sourceinsight主程序

3、定时操作

Handler mHandler = new Handler();

mHandler.postDelayed(new Runnable()
{
@Override
public void run()
{
}
}, 3000);

4、TagSoup 是一个Java开发符合SAX的HTML解析器

5、android-ndk-r8e/build/gmsl/__gmsl:512: *** non-numeric second argument to `wordlist' function: ''.

将AndroidManifest.xml文件先移动到其他地方,编译成功后再mv回来,这样操作果然成功。

6、

程序主动触发系统回收资源
System.runFinalization();
System.gc();
进程真正退出
System.exit(0);

1.不需要后台运行的app需在退出时调用上面的代码,以便系统回收资源和进程真正退出
2.app运行过程中也可以在合适的时候主动触发系统回收资源

基本上是Activity退到后台时加入以下一段代码处理
if (isTaskRoot()) {
System.runFinalization();
System.gc();
System.exit(0);
}

7、退出所有Activity的方法

在BaseActivity类中

private static LinkedList<Activity> activityList = new LinkedList<Activity>();

在onCreate中activityList.add(this);每次进入新的Activity将this指针压入链表,

重写onDestroy()方法移除相应的Activity,activityList.remove(this);

退出所有的Activity时,只要调用finish();方法,并移除所有的Activity就可以了。

8、android系统jni示例

public class ImageDecoder {
static{
System.loadLibrary("mathappliedprodec");
}
public native boolean decode(String path, Bitmap bitmap, int imageType);
public native boolean encode(String path, Bitmap bitmap, int imageType);
}

#include <jni.h>

#ifndef _Included_com_noahedu_dataparser_ImageDecoder
#define _Included_com_noahedu_dataparser_ImageDecoder
#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jboolean JNICALL Java_com_noahedu_dataparser_ImageDecoder_decode
(JNIEnv *, jobject, jstring, jobject, jint);
JNIEXPORT jboolean JNICALL Java_com_noahedu_dataparser_ImageDecoder_encode
(JNIEnv *, jobject, jstring, jobject, jint);


#ifdef __cplusplus
}
#endif
#endif

 注意函数名称命名方式。

java|包名|类名称|函数名称

Java_com_noahedu_dataparser_ImageDecoder_decode

9、jni调试

//在C工程、android工程 调试切换
#ifdef ANDROID
#include <android/log.h>
#define LOG_TAG "mathapplied"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#else
#define LOGI(...) printf(__VA_ARGS__);
#endif

10、jni杂记

jstring path

const char *file = (*env)->GetStringUTFChars(env, path, NULL);

(*env)->ReleaseStringUTFChars(env, path, file);

11、取消代码混淆编译

LOCAL_PROGUARD_ENABLED := disabled

12、使用jar包名称区分大小写

<uses-library
android:name="PenWriterLib"
android:required="false" />

 13、listview背景黑块问题

android:cacheColorHint="#00000000"

12、在android程序中,如何将LogCat上的日志输出到文件?

LogCat存储在circular memory buffers中。 

(1)、可以通过命令来导出Log: 

引用
adb logcat -d > logcat.txt



详细参考 
http://developer.android.com/tools/help/adb.html#logcat 

(2)、在程序中获取Log的方法: 

引用
<uses-permission android:name="android.permission.READ_LOGS" />



Java代码  收藏代码
  1. public class LogTest extends Activity {  
  2.   @Override  
  3.   public void onCreate(Bundle savedInstanceState) {  
  4.     super.onCreate(savedInstanceState);  
  5.     setContentView(R.layout.main);  
  6.     try {  
  7.       Process process = Runtime.getRuntime().exec("logcat -d");  
  8.       BufferedReader bufferedReader = new BufferedReader(  
  9.       new InputStreamReader(process.getInputStream()));  
  10.   
  11.       StringBuilder log=new StringBuilder();  
  12.       String line;  
  13.       while ((line = bufferedReader.readLine()) != null) {  
  14.         log.append(line);  
  15.       }  
  16.       TextView tv = (TextView)findViewById(R.id.textView1);  
  17.       tv.setText(log.toString());  
  18.     } catch (IOException e) {  
  19.     }  
  20.   }  
  21. }  



详细参考 
http://www.helloandroid.com/tutorials/reading-logs-programatically 

原文地址:https://www.cnblogs.com/colife/p/3466975.html