Android notes 2--test and data storage methods under Android

Source: Internet
Author: User

Here's another basic knowledge of Android, several ways to test and store your data under Android.
The corresponding notes are issued synchronously after the later. The usual, use a picture to introduce today's content.
If the picture is not clear, you can right-click the new window.

I. Test 1, classification

Black box testing: The user's point of view, from the input data and output data corresponding to the relationship between the test.
  
White Box testing: Also known as structural testing, transparent box testing, logic-driven testing, or code-based testing.
  
Unit testing: Also known as module testing, is a small piece of code written by a developer to verify that a small, well-defined function of the code being tested is correct.
  
Functional testing: Test the characteristics and operational behavior of a product according to product characteristics, operating description, and user scenarios to determine that they meet the design requirements.
  
Stress test: The subject is assigned a quantitative task and assignment to the observed person to observe the individual's behavior in accomplishing the task.
  
Integration testing: A logical extension of unit testing. Its simplest form is: Two tested units are assembled into one component, and the interface between them is tested

2. Unit Test Framework (JUNIT)

Android code can only be run on the phone in the Dalvik virtual machine, the PC in the JVM will be an error exception. Write test cases and run them by uploading the test code to an Android phone or simulator.

The process of unit testing under 3.Android

1, write business logic code

2, write test cases, one class inherits Androidtestcase

3, write test method public void Testadd () throws Exception

4. Configure the manifest file
Add instrumention under the Mainfest node,
Add uses-library under the application node

Introduction to Logcat under 4,android
Log.v:verbose hint Black
Log.d:debug Debug Blue
Log.i:info Reminder Green
Log.w:warn Warning Orange
Log.e:error Error Red

Second, Android file storage 1, save data to SD card 1, get the external SD card directory:
      Environment.getExternalStorageDirectory()
2, Get Mount Status:
      Environment.getExternalStorageState()
3. Get the remaining SD space:
          Environment.getExternalStorageDirectory().getUsableSpace();
2. Save data to internal storage device (/data/data/package name/)

Apply a private folder that is not accessible by default by other apps. Keep their data safe

Context.getfilesdir () = =/data/data/Package name/files user file directory
Context.getcachedir () = =/data/data/Package name/cache Cache Directory

Third, Android permissions mechanism

Mode_private Private Files
Mode_world_readable Global readable file
Mode_world_writeable Global writable File
Mode_world_readable + mode_world_writeable Global readable writable

Four, Android XML data access (pull parser) 1.xml data analysis
  a. 创建xml解析器          XmlPullParser parser = Xml.newPullParser();  b. 初始化xml解析器, 指定解析哪个流, 以什么编码解析             parser.setInput(is, "utf-8");  c. 解析xml数据             while(type != XmlPullParser.END_DOCUMENT){ // 直到文档的结束          // 读取数据          ...          type = parser.next(); // 到下一个      }  d. 扫尾工作  关闭数据流
2. Creation of XML (serialization)
  a. 创建Xml序列化器     XmlSerializer serializer = Xml.newSerializer();  b. 初始化xml序列化器, 设置输出流, 指定编码集     serializer.setOutput(fos, "utf-8");  c. 写xml数据     // 文档的开头   serializer.startDocument("utf-8", true);   serializer.startTag(null, "info");     serializer.startTag(null, "qq");      serializer.text(qq);      serializer.endTag(null, "qq");      serializer.startTag(null, "pwd");      serializer.text(pwd);      serializer.endTag(null, "pwd");      serializer.endTag(null, "info");          // 文档的结束      serializer.endDocument();  d. fos.close();
Wu, Sharedpreference

Save location:/data/data/Package Name/shared_prefs/xxx.xml

1. Write Data
  初始化SharedPreferences  SharedPreferences sp = this.getSharedPreferences("config", MODE_PRIVATE);  通过SharedPreferences获取编辑器  Editor editor = sp.edit();  写入数据  editor.putString("qq", qq);  editor.putString("pwd", pwd);  editor.putBoolean("isChecked", isChecked);  提交数据  editor.commit();
2. Reading data
     // 1. 初始化SharedPreferences      SharedPreferences sp = this.getSharedPreferences("config", MODE_PRIVATE);        // 2. 读取数据, 设置数据      String qq = sp.getString("qq", "");      boolean isChecked = sp.getBoolean("isChecked", false);

Android notes 2--test and data storage methods under Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.