android 获取指定磁盘内存的工具类【兼容API18以上】,sharedPreference工具类

本文提供两个实用工具类:一是针对Android API 18及以上版本的磁盘信息查询工具,用于获取指定磁盘的总容量及可用空间;二是SharedPreference工具类,简化了应用中保存与读取配置信息的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. android 获取指定磁盘内存的工具类【兼容API18以上】

Const.ANDROID_SDK_INT = 18 api18
package com.apktv.market.util;

import com.apktv.market.consts.Const;

import android.os.Build;
import android.os.StatFs;

public class MemoryInfoUtils {
    
    private static MemoryInfoUtils instance = new MemoryInfoUtils();
    
    private MemoryInfoUtils(){
        
    }
    
    public static MemoryInfoUtils getInstance(){
        
        return instance;
        
    }
    
    /**
     * @param path 磁盘的路径
     * @return 该磁盘的总容量
     */
    public long getTotalSize(String path){
         StatFs sf = new StatFs(path);
         long totalBlockCount;
         long blockSize;
         if (Build.VERSION.SDK_INT >=  Const.ANDROID_SDK_INT) {
             totalBlockCount = sf.getBlockCountLong();
             blockSize = sf.getBlockSizeLong();
         }
         else{
             totalBlockCount = sf.getBlockCount();
             blockSize = sf.getBlockSize();
            
         }
         return (totalBlockCount*blockSize)/1024/1024;
    }
    
    /**
     * @param path 磁盘路径
     * @return 该磁盘剩余可用的容量
     */
    public long getAvaiableSize(String path){
         StatFs sf = new StatFs(path);
         long avaiLableBlockCount;
         long blockSize;
         if (Build.VERSION.SDK_INT >=  Const.ANDROID_SDK_INT) {
             avaiLableBlockCount = sf.getAvailableBlocksLong();
             blockSize = sf.getBlockSizeLong();
         }
         else{
             avaiLableBlockCount = sf.getAvailableBlocks();
             blockSize = sf.getBlockSize();
            
         }
         return (avaiLableBlockCount*blockSize)/1024/1024;
    }
}


2.SharedPreference工具类

package com.hp.lessonhelper.utils;

import com.hp.draftpaper.App.App;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * @author tangdekun
 *         SharedPreferences工具类
 *
 */
public class SharedPreferencesUtils {

	private static SharedPreferencesUtils instance;
	private Context context;
	private static final String FILE_NAME = "share_data";
	private SharedPreferences mPreferences;
	private Editor mEditor;

	private SharedPreferencesUtils() {
		context = App.AppContext;
		mPreferences = context.getSharedPreferences(FILE_NAME,
				Context.MODE_PRIVATE);
		mEditor = mPreferences.edit();
	}

	public static SharedPreferencesUtils getInstance() {
		if (instance == null) {
			instance = new SharedPreferencesUtils();
		}
		return instance;

	}

	public void put(String key, Object object) {

		String type = object.getClass().getSimpleName();
		if ("String".equals(type)) {
			mEditor.putString(key, (String) object);
		} else if ("Integer".equals(type)) {
			mEditor.putInt(key, (Integer) object);
		} else if ("Boolean".equals(type)) {
			mEditor.putBoolean(key, (Boolean) object);
		} else if ("Float".equals(type)) {
			mEditor.putFloat(key, (Float) object);
		} else if ("Long".equals(type)) {
			mEditor.putLong(key, (Long) object);
		}

		mEditor.commit();

	}

	public Object get(String key, Object defaultObject) {
		String type = defaultObject.getClass().getSimpleName();
		if ("String".equals(type)) {
			return mPreferences.getString(key, (String) defaultObject);
		} else if ("Integer".equals(type)) {
			return mPreferences.getInt(key, (Integer) defaultObject);
		} else if ("Boolean".equals(type)) {
			return mPreferences.getBoolean(key, (Boolean) defaultObject);
		} else if ("Float".equals(type)) {
			return mPreferences.getFloat(key, (Float) defaultObject);
		} else if ("Long".equals(type)) {
			return mPreferences.getLong(key, (Long) defaultObject);
		}

		return null;
	}
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值