Android核心基础——Day02_2

来源:互联网 发布:利乐公司待遇 知乎 编辑:程序博客网 时间:2024/05/22 05:12

*8.获得内存状态


看Android源码:怎么下载,以后会讲。


--

Ctrl+L 快速到某一行!


--

到这里,我们就可以通过源码找到了系统的内存存储。


--

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView tvMemoryInfo = (TextView) findViewById(R.id.tv_memory_info);// 获得sd卡的内存状态File sdcardFileDir = Environment.getExternalStorageDirectory();String sdcardMemory = getMemoryInfo(sdcardFileDir);// 获得手机内部存储控件的状态File dataFileDir = Environment.getDataDirectory();String dataMemory = getMemoryInfo(dataFileDir);tvMemoryInfo.setText("SD卡: " + sdcardMemory + "\n手机内部: " + dataMemory);}/** * 根据路径获取内存状态 * @param path * @return */private String getMemoryInfo(File path) {// 获得一个磁盘状态对象        StatFs stat = new StatFs(path.getPath());                long blockSize = stat.getBlockSize();// 获得一个扇区的大小                long totalBlocks = stat.getBlockCount();// 获得扇区的总数                long availableBlocks = stat.getAvailableBlocks();// 获得可用的扇区数量                // 总空间        String totalMemory =  Formatter.formatFileSize(this, totalBlocks * blockSize);        // 可用空间        String availableMemory = Formatter.formatFileSize(this, availableBlocks * blockSize);return "总空间: " + totalMemory + "\n可用空间: " + availableMemory;}}


*9.android下权限

clean data 和clean cache

之前都是存在了data里面,接下来存在cache。


内存,外置存储SDCard。


权限问题:

案例:一个程序往存储空间写数据,另一个程序读第一个程序写进去的数据。


---



--

*10.SharedPreferences使用



*11.xml解析和序列化

使用pull解析:


--

/** * 写xml文件到本地 */private void writeXmlToLocal() {List<Person> personList = getPersonList();// 获得序列化对象XmlSerializer serializer = Xml.newSerializer();try {File path = new File(Environment.getExternalStorageDirectory(), "persons.xml");FileOutputStream fos = new FileOutputStream(path);// 指定序列化对象输出的位置和编码serializer.setOutput(fos, "utf-8");serializer.startDocument("utf-8", true);// 写开始 <?xml version='1.0' encoding='utf-8' standalone='yes' ?>serializer.startTag(null, "persons");// <persons>for (Person person : personList) {// 开始写人serializer.startTag(null, "person");// <person>serializer.attribute(null, "id", String.valueOf(person.getId()));// 写名字serializer.startTag(null, "name");// <name>serializer.text(person.getName());serializer.endTag(null, "name");// </name>// 写年龄serializer.startTag(null, "age");// <age>serializer.text(String.valueOf(person.getAge()));serializer.endTag(null, "age");// </age>serializer.endTag(null, "person");// </person>}serializer.endTag(null, "persons");// </persons>serializer.endDocument();// 结束} catch (Exception e) {e.printStackTrace();}}private List<Person> getPersonList() {List<Person> personList = new ArrayList<Person>();for (int i = 0; i < 30; i++) {personList.add(new Person(i, "wang" + i, 18 + i));}return personList;}

--

读来呢?





0 0
原创粉丝点击