我想在我的应用程序中获取内存和进度条中的已用内存,如下图所示。如何应用请帮助我自定义 android 中使用的进度条。我使用了下载应用程序。
问问题
5111 次
2 回答
2
以下是它的实现方式:
TestActivity.java:
public class TestActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView occupiedSpaceText = (TextView)findViewById(R.id.occupiedSpace);
final TextView freeSpaceText = (TextView)findViewById(R.id.freeSpace);
final ProgressBar progressIndicator = (ProgressBar)findViewById(R.id.indicator);
final float totalSpace = DeviceMemory.getInternalStorageSpace();
final float occupiedSpace = DeviceMemory.getInternalUsedSpace();
final float freeSpace = DeviceMemory.getInternalFreeSpace();
final DecimalFormat outputFormat = new DecimalFormat("#.##");
if (null != occupiedSpaceText) {
occupiedSpaceText.setText(outputFormat.format(occupiedSpace) + " MB");
}
if (null != freeSpaceText) {
freeSpaceText.setText(outputFormat.format(freeSpace) + " MB");
}
if (null != progressIndicator) {
progressIndicator.setMax((int) totalSpace);
progressIndicator.setProgress((int)occupiedSpace);
}
}
/**
* From question http://stackoverflow.com/questions/2652935/android-internal-phone-storage by Lazy Ninja
*/
public static class DeviceMemory {
public static float getInternalStorageSpace() {
StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());
//StatFs statFs = new StatFs("/data");
float total = ((float)statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
return total;
}
public static float getInternalFreeSpace() {
StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());
//StatFs statFs = new StatFs("/data");
float free = ((float)statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
return free;
}
public static float getInternalUsedSpace() {
StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());
//StatFs statFs = new StatFs("/data");
float total = ((float)statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
float free = ((float)statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
float busy = total - free;
return busy;
}
}
}
布局/main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
style="@android:style/Widget.ProgressBar.Horizontal"
android:progressDrawable="@drawable/memory_indicator_progress" />
<TextView
android:id="@+id/occupiedSpace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:gravity="left"
android:layout_marginLeft="5dp"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/freeSpace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:gravity="right"
android:layout_marginRight="5dp"
android:textColor="@android:color/black"
android:textStyle="bold" />
</RelativeLayout>
可绘制/memory_indicator_progress.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<solid android:color="@android:color/holo_green_light" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape android:shape="rectangle">
<solid android:color="@android:color/darker_gray" />
</shape>
</clip>
</item>
</layer-list>
我不确定它是否正是您正在寻找的东西,但是在我的带有 4.1 android 的 xperia v 上,我得到了以下图片:
由于平台不同,您的设备颜色可能会有所不同。
于 2013-03-20T09:15:08.677 回答
0
使用 android.os.Environment 找到内部目录,然后使用 android.os.StatFs 在其上调用 Unix statfs 系统调用。从Android设置应用程序中无耻地窃取:
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);
从这个答案
于 2013-03-20T07:44:53.397 回答