您的位置:首页 > 移动开发 > Android开发

Android编码规范

2015-08-12 14:15 633 查看
转自知乎

写一个抽象的BaseActivity.java,将初始化抽象为setContentView()、findViews()、getData()、showContent()这四个方法,所有的Activity都继承它:

public abstract class BaseActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}

public void init(){
setContentView();
findViews();
getData();
showContent();
}

public abstract void setContentView();
public abstract void findViews();
public abstract void getData();
public abstract void showContent();
}


2. 方法返回值不返回null,返回空值,比如:

public static List<String> getFolderFiles( String folderPath ){
List<String> fileList = new ArrayList<String>( );
if( TextUtils.isEmpty( folderPath ) ){
return fileList;
}

File file = new File( folderPath );
if( file.isDirectory( ) ){
File[] files = file.listFiles( );
if( null != files ){
fileList = new ArrayList<String>( );
for( File subFile : files ){
fileList.add( subFile.getPath( ) );
}
}
}

return fileList;
}


3. 在方法顶部做入参判断,入参异常直接返回,比如:

public static List<String> getAssertsFiles( Context context ){
List<String> assertsFileList = new ArrayList<String>( );
if( null == context ){
return assertsFileList;
}

AssetManager assetManager = context.getAssets();
String[] files = null;
try {
files = assetManager.list("");
assertsFileList = Arrays.asList( files );
} catch (IOException e) {
e.printStackTrace( );
}

return assertsFileList;
}


4. 使用List、Map而不是ArrayList、HashMap声明成员变量、作为返回值、作为入参等等,尽量用抽象而不是具体实现。

5. 在任何时候不使用类的finalize方法释放资源;

6. 在finally中关闭文件流、cursor等等;

7. 封装一个DebugUtils类来管理程序中所有的打印:

public class DebugUtils{
public static final String TAG = "Debug";

private DebugUtils( ){

}

public static void println( String printInfo ){
if( Debug.DEBUG_MODE && null != printInfo ){
System.out.println( printInfo );
}
}

public static void print( String printInfo ){
if( Debug.DEBUG_MODE && null != printInfo ){
System.out.print( printInfo );
}
}

public static void printLogI( String logInfo ){
printLogI( TAG, logInfo );
}

public static void printLogI( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag && null != logInfo ){
Log.i( tag, logInfo );
}
}

public static void printLogE( String logInfo ){
printLogE( TAG, logInfo );
}

public static void printLogE( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag && null != logInfo ){
Log.e( tag, logInfo );
}
}

public static void printLogW( String logInfo ){
printLogW( TAG, logInfo );
}

public static void printLogW( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag && null != logInfo ){
Log.w( tag, logInfo );
}
}

public static void printLogD( String logInfo ){
printLogD( TAG, logInfo );
}

public static void printLogD( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag && null != logInfo ){
Log.d( tag, logInfo );
}
}

public static void printLogV( String logInfo ){
printLogV( TAG, logInfo );
}

public static void printLogV( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag || null != logInfo ){
Log.v( tag, logInfo );
}
}

public static void printLogWtf( String logInfo ){
printLogWtf( TAG, logInfo );
}

public static void printLogWtf( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag && null != logInfo ){
Log.wtf( tag, logInfo );
}
}

public static void showToast( Context context, String toastInfo ){
if( null != context && null != toastInfo ){
Toast.makeText( context, toastInfo, Toast.LENGTH_LONG ).show( );
}
}

public static void showToast( Context context, String toastInfo, int timeLen ){
if( null != context && null != toastInfo && ( timeLen > 0 ) ){
Toast.makeText( context, toastInfo, timeLen ).show( );
}
}

public static void printBaseInfo( ){
if( Debug.DEBUG_MODE ){
StringBuffer strBuffer = new StringBuffer( );
StackTraceElement[ ] stackTrace = new Throwable( ).getStackTrace( );

strBuffer.append( "; class:" ).append( stackTrace[ 1 ].getClassName( ) )
.append( "; method:" ).append( stackTrace[ 1 ].getMethodName( ) )
.append( "; number:" ).append( stackTrace[ 1 ].getLineNumber( ) )
.append( "; fileName:" ).append( stackTrace[ 1 ].getFileName( ) );

println( strBuffer.toString( ) );
}
}

public static void printFileNameAndLinerNumber( ){
if( Debug.DEBUG_MODE ){
StringBuffer strBuffer = new StringBuffer( );
StackTraceElement[ ] stackTrace = new Throwable( ).getStackTrace( );

strBuffer.append( "; fileName:" ).append( stackTrace[ 1 ].getFileName( ) )
.append( "; number:" ).append( stackTrace[ 1 ].getLineNumber( ) );

println( strBuffer.toString( ) );
}
}

public static int printLineNumber( ){
if( Debug.DEBUG_MODE ){
StringBuffer strBuffer = new StringBuffer( );
StackTraceElement[ ] stackTrace = new Throwable( ).getStackTrace( );

strBuffer.append( "; number:" ).append( stackTrace[ 1 ].getLineNumber( ) );

println( strBuffer.toString( ) );
return stackTrace[ 1 ].getLineNumber( );
}else{
return 0;
}
}

public static void printMethod( ){
if( Debug.DEBUG_MODE ){
StringBuffer strBuffer = new StringBuffer( );
StackTraceElement[ ] stackTrace = new Throwable( ).getStackTrace( );

strBuffer.append( "; number:" ).append( stackTrace[ 1 ].getMethodName( ) );

println( strBuffer.toString( ) );
}
}

public static void printFileNameAndLinerNumber( String printInfo ){
if( null == printInfo || !Debug.DEBUG_MODE ){
return;
}
StringBuffer strBuffer = new StringBuffer( );
StackTraceElement[ ] stackTrace = new Throwable( ).getStackTrace( );

strBuffer.append( "; fileName:" ).append( stackTrace[ 1 ].getFileName( ) )
.append( "; number:" ).append( stackTrace[ 1 ].getLineNumber( ) ).append( "\n" )
.append( ( null != printInfo ) ? printInfo : "" );

println( strBuffer.toString( ) );
}

public static void showStrictMode( ) {
if (DebugUtils.Debug.DEBUG_MODE) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
}
}

public static void d(String tag, String msg){
if(DebugUtils.Debug.DEBUG_MODE){
Log.d(tag, msg);
}
}

public class Debug{
public static final boolean DEBUG_MODE = true;
}
}


8. 静态类的构造方法私有化,具体实例参见7;

9. 没有必要用硬件加速的页面在AndroidMainfest.xml中将其关掉

android:hardwareAccelerated="false"


10. 在没有使用到多点触控的页面,通过在主题中设置下列两个属性将多点触控关掉:

<item name="android:windowEnableSplitTouch">false</item>
<item name="android:splitMotionEvents">false</item>


11. 定义一个防止视图重复点击的静态类,在需要做防止重复点击的地方加上该判断:

public class BtnClickUtils {
private static long mLastClickTime = 0;

private BtnClickUtils( ){

}

public static boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - mLastClickTime;
if ( 0 < timeD && timeD < 500) {
return true;
}

mLastClickTime = time;

return false;
}
}


12. 在布局中使用LinearLayout的android:weight属性时,将view的android:layout_width属性设置为0dp;

13. 涉及到APP的核心数据全部加密处理,在使用的时候解密;

14. 如果你的IDE是Eclipse,用好这几个快捷键,爽歪歪滴:
CTRL + SHIFT + O
ALT + SHIFT + M
ALT + SHIFT + S
CTRL + O
CTRL + /
ALT + /
CTRL + D
CTRL + K
ALT + → 和 ALT + 左箭头
CTRL + SHIFT + X 和 CTRL + SHIFT + Y
F5、F6、F7、F8
F11
CTRL + M

//////////////////////////////////////////另一篇////////////////////////////////////////////////////////////////////

一、Android编码规范


1.java代码中不出现中文,最多注释中可以出现中文;

2.局部变量命名、静态成员变量命名:只能包含字母,单词首字母出第一个都为大写,其他字母都为小写;

3.常量命名:只能包含字母和_,字母全部大写,单词之间用_隔开;

4.layout中的id命名:命名模式为:view缩写_模块名称_view的逻辑名称

view的缩写详情如下

LayoutView:lv

RelativeView:rv

TextView:tv

ImageView:iv

ImageButton:im

Button:btn

5.activity中的view变量命名

命名模式为:逻辑名称+view缩写

建议:如果layout文件很复杂,建议将layout分成多个模块,每个模块定义一个moduleViewHolder,其成员变量包含所属view

6.strings.xml中的id命名:

命名模式:activity名称_功能模块名称_逻辑名称 activity名称_逻辑名称 common_逻辑名称
strings.xml中,使用activity名称注释,将文件内容区分开来
7.drawable中的图片命名
命名模式:activity名称_逻辑名称/common_逻辑名称
7.styles.xml:将layout中不断重现的style提炼出通用的style通用组件,放到styles.xml中;
8.使用layer-list和selector
9.图片尽量分拆成多个可重用的图片
10.服务端可以实现的,就不要放在客户端
11.引用第三方库要慎重,避免应用大容量的第三方库,导致客户端包非常大
12.处理应用全局异常和错误,将错误以邮件的形式发送给服务端
13.图片的.9处理
14.使用静态变量方式实现界面间共享要慎重
15.Log(系统名称 模块名称 接口名称,详细描述)
16.单元测试(逻辑测试、界面测试)
17.不要重用父类的handler,对应一个类的handler也不应该让其子类用到,否则会导致message.what冲突
18.activity中在一个View.OnClickListener中处理所有的逻辑
19.strings.xml中使用%1$s实现字符串的通配
20.如果多个Activity中包含共同的UI处理,那么可以提炼一个CommonActivity,把通用部分叫由它来处理,其他activity只要继承它即可
21.使用button+activitgroup实现tab效果时,使用Button.setSelected(true),确保按钮处于选择状态,并使activitygroup的当前activity与该button对应
22.如果所开发的为通用组件,为避免冲突,将drawable/layout/menu/values目录下的文件名增加前缀
23.数据一定要效验,例如
字符型转数字型,如果转换失败一定要有缺省值;
服务端响应数据是否有效判断;

二、Android性能优化

1.http用gzip压缩,设置连接超时时间和响应超时时间

http请求按照业务需求,分为是否可以缓存和不可缓存,那么在无网络的环境中,仍然通过缓存的httpresponse浏览部分数据,实现离线阅读。

2.listview 性能优化

1).复用convertView

在getItemView中,判断convertView是否为空,如果不为空,可复用。如果couvertview中的view需要添加listerner,代码一定要在if(convertView==null){}之外。

2).异步加载图片

item中如果包含有webimage,那么最好异步加载

3).快速滑动时不显示图片

当快速滑动列表时(SCROLL_STATE_FLING),item中的图片或获取需要消耗资源的view,可以不显示出来;而处于其他两种状态(SCROLL_STATE_IDLE 和SCROLL_STATE_TOUCH_SCROLL),则将那些view显示出来

3.使用线程池,分为核心线程池和普通线程池,下载图片等耗时任务放置在普通线程池,避免耗时任务阻塞线程池后,导致所有异步任务都必须等待

4.异步任务,分为核心任务和普通任务,只有核心任务中出现的系统级错误才会报错,异步任务的ui操作需要判断原activity是否处于激活状态

5.尽量避免static成员变量引用资源耗费过多的实例,比如Context

6.使用WeakReference代替强引用,弱引用可以让您保持对对象的引用,同时允许GC在必要时释放对象,回收内存。对于那些创建便宜但耗费大量内存的对象,即希望保持该对象,又要在应用程序需要时使用,同时希望GC必要时回收时,可以考虑使用弱引用。

7.超级大胖子Bitmap

及时的销毁(Activity的onDestroy时,将bitmap回收)

设置一定的采样率

巧妙的运用软引用

drawable对应resid的资源,bitmap对应其他资源8.保证Cursor 占用的内存被及时的释放掉,而不是等待GC来处理。并且 Android明显是倾向于编程者手动的将Cursor close掉

9.线程也是造成内存泄露的一个重要的源头。线程产生内存泄露的主要原因在于线程生命周期的不可控

10.如果ImageView的图片是来自网络,进行异步加载

11.应用开发中自定义View的时候,交互部分,千万不要写成线程不断刷新界面显示,而是根据TouchListener事件主动触发界面的更新

三、AndroidUI优化

1.layout组件化,尽量使用merge及include复用

2.使用styles,复用样式定义

3.软键盘的弹出控制,不要让其覆盖输入框

4.数字、字母和汉字混排占位问题:将数字和字母全角化。由于现在大多数情况下我们的输入都是半角,所以 字母和数字的占位无法确定,但是一旦全角化之后,数字、字母的占位就和一个汉字的占位相同了,这样就可以避免由于占位导致的排版问题。

5.英文文档排版:textview自动换行时要保持单词的完整性,解决方案是计算字符串长度,然后手动设定每一行显示多少个字母并加上‘n‘

6.复杂布局使用RelativeLayout

7.自适应屏幕,使用dp替代pix

8.使用android:layout_weight或者TableLayout制作等分布局

写一个抽象的BaseActivity.java,将初始化抽象为setContentView()、findViews()、getData()、showContent()这四个方法,所有的Activity都继承它:

public abstract class BaseActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}

public void init(){
setContentView();
findViews();
getData();
showContent();
}

public abstract void setContentView();
public abstract void findViews();
public abstract void getData();
public abstract void showContent();
}


2. 方法返回值不返回null,返回空值,比如:

public static List<String> getFolderFiles( String folderPath ){
List<String> fileList = new ArrayList<String>( );
if( TextUtils.isEmpty( folderPath ) ){
return fileList;
}

File file = new File( folderPath );
if( file.isDirectory( ) ){
File[] files = file.listFiles( );
if( null != files ){
fileList = new ArrayList<String>( );
for( File subFile : files ){
fileList.add( subFile.getPath( ) );
}
}
}

return fileList;
}


3. 在方法顶部做入参判断,入参异常直接返回,比如:

public static List<String> getAssertsFiles( Context context ){
List<String> assertsFileList = new ArrayList<String>( );
if( null == context ){
return assertsFileList;
}

AssetManager assetManager = context.getAssets();
String[] files = null;
try {
files = assetManager.list("");
assertsFileList = Arrays.asList( files );
} catch (IOException e) {
e.printStackTrace( );
}

return assertsFileList;
}


4. 使用List、Map而不是ArrayList、HashMap声明成员变量、作为返回值、作为入参等等,尽量用抽象而不是具体实现。

5. 在任何时候不使用类的finalize方法释放资源;

6. 在finally中关闭文件流、cursor等等;

7. 封装一个DebugUtils类来管理程序中所有的打印:

public class DebugUtils{
public static final String TAG = "Debug";

private DebugUtils( ){

}

public static void println( String printInfo ){
if( Debug.DEBUG_MODE && null != printInfo ){
System.out.println( printInfo );
}
}

public static void print( String printInfo ){
if( Debug.DEBUG_MODE && null != printInfo ){
System.out.print( printInfo );
}
}

public static void printLogI( String logInfo ){
printLogI( TAG, logInfo );
}

public static void printLogI( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag && null != logInfo ){
Log.i( tag, logInfo );
}
}

public static void printLogE( String logInfo ){
printLogE( TAG, logInfo );
}

public static void printLogE( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag && null != logInfo ){
Log.e( tag, logInfo );
}
}

public static void printLogW( String logInfo ){
printLogW( TAG, logInfo );
}

public static void printLogW( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag && null != logInfo ){
Log.w( tag, logInfo );
}
}

public static void printLogD( String logInfo ){
printLogD( TAG, logInfo );
}

public static void printLogD( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag && null != logInfo ){
Log.d( tag, logInfo );
}
}

public static void printLogV( String logInfo ){
printLogV( TAG, logInfo );
}

public static void printLogV( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag || null != logInfo ){
Log.v( tag, logInfo );
}
}

public static void printLogWtf( String logInfo ){
printLogWtf( TAG, logInfo );
}

public static void printLogWtf( String tag, String logInfo ){
if( Debug.DEBUG_MODE && null != tag && null != logInfo ){
Log.wtf( tag, logInfo );
}
}

public static void showToast( Context context, String toastInfo ){
if( null != context && null != toastInfo ){
Toast.makeText( context, toastInfo, Toast.LENGTH_LONG ).show( );
}
}

public static void showToast( Context context, String toastInfo, int timeLen ){
if( null != context && null != toastInfo && ( timeLen > 0 ) ){
Toast.makeText( context, toastInfo, timeLen ).show( );
}
}

public static void printBaseInfo( ){
if( Debug.DEBUG_MODE ){
StringBuffer strBuffer = new StringBuffer( );
StackTraceElement[ ] stackTrace = new Throwable( ).getStackTrace( );

strBuffer.append( "; class:" ).append( stackTrace[ 1 ].getClassName( ) )
.append( "; method:" ).append( stackTrace[ 1 ].getMethodName( ) )
.append( "; number:" ).append( stackTrace[ 1 ].getLineNumber( ) )
.append( "; fileName:" ).append( stackTrace[ 1 ].getFileName( ) );

println( strBuffer.toString( ) );
}
}

public static void printFileNameAndLinerNumber( ){
if( Debug.DEBUG_MODE ){
StringBuffer strBuffer = new StringBuffer( );
StackTraceElement[ ] stackTrace = new Throwable( ).getStackTrace( );

strBuffer.append( "; fileName:" ).append( stackTrace[ 1 ].getFileName( ) )
.append( "; number:" ).append( stackTrace[ 1 ].getLineNumber( ) );

println( strBuffer.toString( ) );
}
}

public static int printLineNumber( ){
if( Debug.DEBUG_MODE ){
StringBuffer strBuffer = new StringBuffer( );
StackTraceElement[ ] stackTrace = new Throwable( ).getStackTrace( );

strBuffer.append( "; number:" ).append( stackTrace[ 1 ].getLineNumber( ) );

println( strBuffer.toString( ) );
return stackTrace[ 1 ].getLineNumber( );
}else{
return 0;
}
}

public static void printMethod( ){
if( Debug.DEBUG_MODE ){
StringBuffer strBuffer = new StringBuffer( );
StackTraceElement[ ] stackTrace = new Throwable( ).getStackTrace( );

strBuffer.append( "; number:" ).append( stackTrace[ 1 ].getMethodName( ) );

println( strBuffer.toString( ) );
}
}

public static void printFileNameAndLinerNumber( String printInfo ){
if( null == printInfo || !Debug.DEBUG_MODE ){
return;
}
StringBuffer strBuffer = new StringBuffer( );
StackTraceElement[ ] stackTrace = new Throwable( ).getStackTrace( );

strBuffer.append( "; fileName:" ).append( stackTrace[ 1 ].getFileName( ) )
.append( "; number:" ).append( stackTrace[ 1 ].getLineNumber( ) ).append( "\n" )
.append( ( null != printInfo ) ? printInfo : "" );

println( strBuffer.toString( ) );
}

public static void showStrictMode( ) {
if (DebugUtils.Debug.DEBUG_MODE) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
}
}

public static void d(String tag, String msg){
if(DebugUtils.Debug.DEBUG_MODE){
Log.d(tag, msg);
}
}

public class Debug{
public static final boolean DEBUG_MODE = true;
}
}


8. 静态类的构造方法私有化,具体实例参见7;

9. 没有必要用硬件加速的页面在AndroidMainfest.xml中将其关掉

android:hardwareAccelerated="false"


10. 在没有使用到多点触控的页面,通过在主题中设置下列两个属性将多点触控关掉:

<item name="android:windowEnableSplitTouch">false</item>
<item name="android:splitMotionEvents">false</item>


11. 定义一个防止视图重复点击的静态类,在需要做防止重复点击的地方加上该判断:

public class BtnClickUtils {
private static long mLastClickTime = 0;

private BtnClickUtils( ){

}

public static boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - mLastClickTime;
if ( 0 < timeD && timeD < 500) {
return true;
}

mLastClickTime = time;

return false;
}
}


12. 在布局中使用LinearLayout的android:weight属性时,将view的android:layout_width属性设置为0dp;

13. 涉及到APP的核心数据全部加密处理,在使用的时候解密;

14. 如果你的IDE是Eclipse,用好这几个快捷键,爽歪歪滴:
CTRL + SHIFT + O
ALT + SHIFT + M
ALT + SHIFT + S
CTRL + O
CTRL + /
ALT + /
CTRL + D
CTRL + K
ALT + → 和 ALT + 左箭头
CTRL + SHIFT + X 和 CTRL + SHIFT + Y
F5、F6、F7、F8
F11
CTRL + M
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息