Android优化 工具类


原地址:http://www.zhihu.com/question/27227425#answer-9778481
  1. 写一个抽象的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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
常用的Android Studio工具类有以下几个: 1. AndroidUniqueID: 这是一个用于获取Android设备唯一标识符的工具类,可以通过GitHub链接(https://github.com/appdevzhang/AndroidUniqueID)找到详细使用方法。 2. Lazy android: 这是一个方便快捷的Android工具类,通过GitHub链接(https://github.com/l123456789jy/Lazy android)可以了解它的具体功能和用法。 3. Utils-Everywhere: 这是一个Android各种工具类的集合,通过GitHub链接(https://github.com/SenhLinsh/Utils-Everywhere)可以查看所有可用的工具类和使用方法。 这些工具类都是为了方便开发者在Android Studio中进行开发而设计的,可以提高开发效率和代码质量。同时,还可以使用Lint工具来进行静态代码检查,找出代码结构和质量问题,并提供解决方案。通过Android Studio自带的Lint功能,可以进行一些常见的代码优化,去除多余的资源等。 可以通过这个(https://blog.csdn.net/ouyang_peng/article/details/80374867)链接来了解更多关于Lint工具的配置和使用方法。 除了Lint工具,还有其他的静态代码检查框架,如FindBugs、PMD和Checkstyle等,它们可以检查Java源文件或class文件的代码质量和代码风格。但在Android开发中,我们通常会选择使用Lint框架,因为它提供了强大的功能、扩展性和与Android Studio、Android Gradle插件的原生支持。此外,Lint框架还提供了许多有用的Android相关检查规则,而且有Google官方的支持,在Android开发工具的升级中也会得到完善。 你可以通过这个链接(https://blog.csdn.net/MeituanTech/article/details/79922364)了解更多关于Lint框架的使用和优势。 总结来说,Android Studio常用的工具类包括AndroidUniqueID、Lazy android和Utils-Everywhere等,而Lint工具则可以帮助我们进行静态代码检查和优化

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值