在手机安装应用时,系统会自动在/data/data目录下面以应用的主包名创建一个目录,默认还会在最后的那个目录里面创建一个lib目录。下面看一下系统获取存储空间的代码:
来自android2.3.3系统源代码/packages/apps/Settings/src/com/android/settings/applications/ManageApplications.java
531.mDataFileStats = new StatFs("/data");
532.mSDCardFileStats = new StatFs(Environment.getExternalStorageDirectory().toString());
<pre name="code" class="java">748.mDataFileStats.restat("/data");
try {
totalStorage = (long)mDataFileStats.getBlockCount() *
mDataFileStats.getBlockSize();
freeStorage = (long) mDataFileStats.getAvailableBlocks() *
mDataFileStats.getBlockSize();
} catch (IllegalArgumentException e) {
}
final int N = mApplicationsAdapter.getCount();
for (int i=0; i<N; i++) {
ApplicationsState.AppEntry ae = mApplicationsAdapter.getAppEntry(i);
appStorage += ae.codeSize + ae.dataSize;
}
freeStorage += mApplicationsState.sumCacheSizes();<pre name="code" class="java"><pre>
734.mSDCardFileStats.restat(Environment.getExternalStorageDirectory().toString());
try {
totalStorage = (long)mSDCardFileStats.getBlockCount() * mSDCardFileStats.getBlockSize();
freeStorage = (long) mSDCardFileStats.getAvailableBlocks() * mSDCardFileStats.getBlockSize();
} catch (IllegalArgumentException e) {
// use the old value of mFreeMem
}
第531行代码是/data目录下面的空间,第532行是检查SD卡的空间。
“存储空间不足”data过小的通知分析DeviceStorageMonitorService类
位置
DeviceStorageMonitorService.java (frameworks\base\services\java\com\android\server\)
一. 三个关键变量:
private static final int DEFAULT_THRESHOLD_PERCENTAGE = 10; private static final int DEFAULT_FULL_THRESHOLD_BYTES = 1*1024*1024; // 1MB private static final long DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD = 2 * 1024 * 1024; // 2MB
二. 参数调用的地方
在整个android目录中, 三个常量被调用的地方, 都只是在DeviceStorageMonitorService.java中
DEFAULT_THRESHOLD_PERCENTAGE(10) : private long getMemThreshold(); return 1300234 * 10 = 13,002,340 DEFAULT_FULL_THRESHOLD_BYTES(10M) : private int getMemFullThreshold() return 10,485,760 DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD(5M=5,242,880) : private final void restatDataDir(); threshold = 5,242,880 delta = mFreeMem = 101,482,696
三. 函数被调用的地方
DeviceStorageMonitorService构造中
private long mMemLowThreshold = getMemThreshold();
四. DeviceStorageMonitorService的调用追踪
private final void checkMemory(boolean checkCache) -> 判断() -> private final void sendNotification()
在sendNotification()中将剩余内存的值存入intent中, 然后广播出去, 广播类型为ACTION_MANAGE_PACKAGE_STORAGE
此通知的标题为 “存储空间不足(Low on space)”
通知的内容为 “手机内存空间所剩不多了(Phone storage space is getting low)”
五. DEFAULT_FULL_THRESHOLD_BYTES的调用追踪
private final void checkMemory(boolean checkCache) -> 判断() -> private final void sendFullNotification() -> sendStickyBroadcast(intent.ACTION_DEVICE_STORAGE_FULL)
若data目录小于DEFAULT_FULL_THRESHOLD_BYTES, 则不在接收短信.
六. DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD 调用追踪
private final void checkMemory(boolean checkCache) -> EventLog.writeEvent(EventLogTags.FREE_STORAGE_CHANGED, mFreeMem)
经过jwisp烧录到真机上发现, 真正有可能有用的是百分比报警, 那只需要设置下面这个参数即可:
DEFAULT_THRESHOLD_PERCENTAGE