ToolsFinal简介

添加一些android开发中常用的一些工具类。将会持续更新,敬请期待。

下载ToolsFinal


下载这个JAR 或者通过Gradle抓取:

compile 'cn.finalteam:toolsfinal:1.1.5'

Android开发常用的工具类

BitmapUtils

Logger

Logger.d(...);
Logger.i(...);
Logger.e(...);
Logger.w(...);
Logger.v(...);
Logger.a(...);
...

StringUtils

CrashHanlder

CrashHandler.getInstance().init(this)
                .setOnCrashListener(...)
                .setCrashSave(true)
                .setCrashSaveTargetFolder("/sdcard/crash");

ActivityManager

//在BaseActivity中添加
//onCreate
ActivityManager.getActivityManager().addActivity(this);
//onDestory
ActivityManager.getActivityManager().finishActivity(this);

//finish所有页面
ActivityManager.getActivityManager().finishAllActivity(this);
//finish所有页面和kill app
ActivityManager.getActivityManager().appExit(this);

AppCacheUtils

//存储
AppCacheUtils.getInstance(context).put(key, int);
AppCacheUtils.getInstance(context).put(key, String);
AppCacheUtils.getInstance(context).put(key, char);
AppCacheUtils.getInstance(context).put(key, float);
AppCacheUtils.getInstance(context).put(key, double);
AppCacheUtils.getInstance(context).put(key, boolean);
AppCacheUtils.getInstance(context).put(key, byte[]);
AppCacheUtils.getInstance(context).put(key, JSONObject);
AppCacheUtils.getInstance(context).put(key, Serializable);
...
//获取
AppCacheUtils.getInstance(context).getInt(key, intDefault);
AppCacheUtils.getInstance(context).getString(key, stringDefault);
AppCacheUtils.getInstance(context).getChar(key, charDefault);
AppCacheUtils.getInstance(context).getFloat(key, floatDefault);
AppCacheUtils.getInstance(context).getDouble(key, doubleDefault);
AppCacheUtils.getInstance(context).getBoolean(key, booleanDefault);
AppCacheUtils.getInstance(context).getJSONObject(key);
AppCacheUtils.getInstance(context).getObject(key);
...
//清除所有缓冲
AppCacheUtils.get(context).clear();
//移除某个缓存
AppCacheUtils.get(context).remove(key);

DateUtils

DeviceUtils

FileUtils

 FileUtils.readFile(String, String)//read file
 FileUtils.readFileToList(String, String)// read file to string list
 FileUtils.writeFile(String, String, boolean)// write file from String
 FileUtils.writeFile(String, String)// write file from String
 FileUtils.writeFile(String, List, boolean)// write file from String List
 FileUtils.writeFile(String, List)// write file from String List
 FileUtils.writeFile(String, InputStream)// write file
 FileUtils.writeFile(String, InputStream, boolean)// write file
 FileUtils.writeFile(File, InputStream)// write file
 FileUtils.writeFile(File, InputStream, boolean)// write file

 FileUtils.moveFile(File, File)// or FileUtils.moveFile(String, String)
 FileUtils.copyFile(String, String)
 FileUtils.getFileExtension(String)
 FileUtils.getFileName(String)
 FileUtils.getFileNameWithoutExtension(String)
 FileUtils.getFileSize(String)
 FileUtils.deleteFile(String)
 FileUtils.isFileExist(String)
 FileUtils.isFolderExist(String)
 FileUtils.makeFolders(String)
 FileUtils.makeDirs(String)

StorageUtils

File cacheDir = StorageUtils.getCacheDirectory(context);///Android/data/packageName/cache
....

ExternalStorage

Map<String, File> storageMap = ExternalStorage.getAllStorageLocations();

ManifestUtils

ManifestUtils.getMetaData(context, metaKey);
ManifestUtils.getChannelNo(context, channelKey);
ManifestUtils.getVersionName(context);
ManifestUtils.getVersionCode(context);

ApkUtils

ApkUtils.getChannelFromApk(context, channelPrefix)//META-INF识别渠道
ApkUtils.install(context, apkFile);//安装
ApkUtils.uninstall(context, packageName);//卸载

JsonFormatUtils

String formatJson = JsonFormatUtils.formatJson(json);

JsonValidator

boolean valid = JsonValidator.validate(json);

定时器

解决系统的CountDownTimer cancel失效问题

    new CountdownTimer(30000, 1000) {
         public void onTick(long millisUntilFinished) {
            mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
         }
         public void onFinish() {
            mTextField.setText("done!");
         }
     }.start();

ResourceUtils

ResourceUtils.getLayoutId(context, resName);
ResourceUtils.getStringId(context, resName);
ResourceUtils.getDrawableId(context, resName);
ResourceUtils.getMipmapId(context, resName);
ResourceUtils.getStyleId(context, resName);
ResourceUtils.getStyleableId(context, resName);
ResourceUtils.getAnimId(context, resName);
ResourceUtils.getId(context, resName);
ResourceUtils.getColorId(context, resName);

ShellUtils

ShellUtils.execCommand(String, boolean)
ShellUtils.execCommand(String, boolean, boolean)
ShellUtils.execCommand(List, boolean)
ShellUtils.execCommand(List, boolean, boolean)
ShellUtils.execCommand(String[], boolean)
ShellUtils.execCommand(String[], boolean, boolean)

加/解密和编码

MD5

String result = MD5Coder.getMD5Code(source);

Base64Util

//解码
byte[] srcBytes = Base64Coder.decode(byte[] decodeBytes, flag)
byte[] srcBytes = Base64Coder.decode(String decodeString, flag)
byte[] srcBytes = Base64Coder.decode(byte[] decodeBytes, int offset, int len, int flags)
...
//编码
byte[] decodeBytes = Base64Coder.encode(byte[] src, flag)
byte[] decodeBytes = Base64Coder.encode(String src, flag)
String decodeString = Base64Coder.encodeToString(byte[] src, flag)
byte[] decodeBytes = Base64Coder.encode(byte[] src, int offset, int len, int flags)
...

AESCoder

//加密
String password = "password";
String message = "hello world"; 
try {
    String encryptedMsg = AESCoder.encrypt(password, message);
}catch (GeneralSecurityException e){
  //handle error
}

//解密
String password = "password";
String encryptedMsg = "2B22cS3UC5s35WBihLBo8w==";
try {
    String messageAfterDecrypt = AESCoder.decrypt(password, encryptedMsg);
}catch (GeneralSecurityException e){
 //handle error - could be due to incorrect password or tampered encryptedMsg
}

DESCoder

//加密
byte[] encrypt = DESCoder.encrypt("Hello World".getBytes(), "password");
Logger.d("encrypt=" + HexCoder.encodeHexString(encrypt));
//解密
byte[] decrypt = DESCoder.decrypt(encrypt, "password");
Logger.d("decrypt=" + new String(decrypt));

DES3Coder

//加密
byte[] encrypt = DES3Coder.encryptMode("Hello World".getBytes(), "password");
Logger.d("encrypt=" + HexCoder.encodeHexString(encrypt));
//解密
byte[] decrypt = DES3Coder.decryptMode(encrypt, "password");
Logger.d("decrypt=" + new String(decrypt));

HexCoder

//byte[]转16进制
char[] encodeHex(byte[] data)
//byte[]转16进制(大写)
char[] encodeHex(byte[] data, boolean toLowerCase)
//byte[]转16进制字符输出
String encodeHexString(byte[] data)
....

EncodeUtils

//把16进制字符串转换成字节数组
EncodeUtils.hexStringToByte(String hex)
//把字节数组转换成16进制字符串
EncodeUtils.bytesToHexString(byte[] bArray)
//BCD码转为10进制串(阿拉伯数据)
EncodeUtils.bcd2Str(byte[] bytes)
//10进制串转为BCD码
EncodeUtils.str2Bcd(String asc)
//BCD码转ASC码
EncodeUtils.bcd2asc(byte[] bytes)
//将字符串编码成16进制数字
EncodeUtils.encodeHex(String str)
//将16进制数字解码成字符串
EncodeUtils.decodeHex(String bytes)

RSACoder

//生成密钥对
KeyPair key = RSACoder.generateKeyPair()
//生成公钥
RSAPublicKey key = RSACoder.generateRSAPublicKey(byte[] modulus, byte[] publicExponent)
//生成私钥
RSAPrivateKey key = RSACoder.generateRSAPrivateKey(byte[] modulus, byte[] privateExponent)
//加密
byte[] result = RSACoder.encrypt(Key key, byte[] data,PADDING padding)
//公钥加密
byte[] result = RSACoder.encryptByPublicKey(byte[] publicKey, byte[] data,PADDING padding)
//私钥加密
byte[] result = RSACoder.encryptByPrivateKey(byte[] publicKey, byte[] privateKey, byte[] data,PADDING padding)
//解密
byte[] result = RSACoder.decrypt(Key key, byte[] data,PADDING padding)
//公钥解密
byte[] result = RSACoder.decryptByPublicKey(byte[] publicKey, byte[] data,PADDING padding)
//私钥解密
byte[] result = RSACoder.decryptByPrivateKey(byte[] publicKey, byte[] privateKey, byte[] data,PADDING padding)

Adapter

ViewHolderAdapter

BaseAdapter ViewHolder优化版Adapter

FragmentAdapter

Fragment page adapter

RecyclingPagerAdapter

可回收的PagerAdapter

权限

大家根据自己使用了哪些工具,分别添加相关权限即可

    <!--在SDCard中创建与删除文件权限-->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <!--往SDCard写入数据权限-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <!--从SDCard读取数据权限-->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <!--读取设备信息权限-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <!--获取WIFI权限-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!--获取手机任务信息(进程,app列表)-->
    <uses-permission android:name="android.permission.GET_TASKS"/>
    <!--检查是否wifi网络 (CrashHanler保存日志信息用到)-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.