數(shù)據(jù)的存儲
1.測試的相關(guān)概念 (了解)
SUV 好的軟件不是開發(fā)出來的是測試出來的
jd 黑客
當(dāng)當(dāng): -10
1.測試是否知道源代碼
黑盒測試 不知道代碼
白盒測試 知道代碼
2.按照測試的粒度
方法測試
單元測試 Junit
集成測試
系統(tǒng)測試
3.按照測試的暴力程度
冒煙測試 硬件
壓力測試 12306
monkey測試: adb shell下的一個測試指令。 adb shell + monkey -p packagename count;
2.單元測試(了解,會用即可)
1.創(chuàng)建一個類集成AndroidTestCase,那么該類就具備單元測試的功能。
2.需要在androidmanifest.xml中的application節(jié)點(diǎn)下配置一個uses-library;
<uses-library android:name="android.test.runner" />
3.需要在androidmanifest.xml中的manifest節(jié)點(diǎn)下配置一個instrumentation;targetPackage:需要測試的工程的包名。
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.itheima.junit" />
4.如果不知道怎么配置androidmanifest.xml,可以新建一個android test project工程,會自動配置.
3.Logcat日志貓工具的使用 (會用即可)
包括五種級別,可以添加過濾器過濾日志信息。能夠幫助我們觀察程序運(yùn)行的狀態(tài)。
e:
w:
i:
d:
v:
在公司開發(fā)中一般打印日志用Log類,通常會封裝一個LogUtils,通過開關(guān)來控制日志信息的打印。
4.把數(shù)據(jù)存儲到文件(login案例) android 下的數(shù)據(jù)存儲
1.寫布局
LinearLayout + RelativeLayout
2.寫業(yè)務(wù)邏輯
a.找到相應(yīng)控件
b.設(shè)置按鈕的點(diǎn)擊事件
c.在onclick方法中,獲取用戶輸入的用戶名密碼和是否記住密碼
d.判斷用戶名密碼是否為空,不為空請求服務(wù)器(省略,默認(rèn)請求成功)
e.判斷是否記住密碼,如果記住,將用戶名密碼保存本地。????
f.回顯用戶名密碼 ??
//通過context對象獲取私有目錄,/data/data/packagename/filse
context.getFileDir().getPath()
5.存儲到SD卡,獲取SD的大小及可用空間 (重點(diǎn))
使用Sdcard注意事項(xiàng):
1.權(quán)限問題:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.硬性編碼問題:通過 Environment可以獲取sdcard的路徑
Environment.getExternalStorageDirectory().getPath();
3.使用前需要判斷sdcard狀態(tài)
if(!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)){
//sdcard狀態(tài)是沒有掛載的情況
Toast.makeText(mContext, "sdcard不存在或未掛載", Toast.LENGTH_SHORT).show();
return ;
}
4.需要判斷sdcard剩余空間
//判斷sdcard存儲空間是否滿足文件的存儲
File sdcard_filedir = Environment.getExternalStorageDirectory();//得到sdcard的目錄作為一個文件對象
long usableSpace = sdcard_filedir.getUsableSpace();//獲取文件目錄對象剩余空間
long totalSpace = sdcard_filedir.getTotalSpace();
//將一個long類型的文件大小格式化成用戶可以看懂的M,G字符串
String usableSpace_str = Formatter.formatFileSize(mContext, usableSpace);
String totalSpace_str = Formatter.formatFileSize(mContext, totalSpace);
if(usableSpace < 1024 * 1024 * 200){//判斷剩余空間是否小于200M
Toast.makeText(mContext, "sdcard剩余空間不足,無法滿足下載;剩余空間為:"+usableSpace_str, Toast.LENGTH_SHORT).show();
return ;
}
/data/data: context.getFileDir().getPath();
是一個應(yīng)用程序的私有目錄,只有當(dāng)前應(yīng)用程序有權(quán)限訪問讀寫,其他應(yīng)用無權(quán)限訪問。一些安全性要求比較高的數(shù)據(jù)存放在該目錄,一般用來存放size比較小的數(shù)據(jù)。
/sdcard: Enviroment.getExternalStorageDirectory().getPath();
是一個外部存儲目錄,只用應(yīng)用聲明了<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>的一個權(quán)限,就可以訪問讀寫sdcard目錄;所以一般用來存放一些安全性不高的數(shù)據(jù),文件size比較大的數(shù)據(jù)。
7.文件的權(quán)限概念 (了解)
//通過context對象獲取一個私有目錄的文件讀取流 /data/data/packagename/files/userinfoi.txt
FileInputStream fileInputStream = context.openFileInput("userinfo.txt");
//通過context對象得到私有目錄下一個文件寫入流; name : 私有目錄文件的名稱 mode: 文件的操作模式, 私有,追加,全局讀,全局寫
FileOutputStream fileOutputStream = context.openFileOutput("userinfo.txt", Context.MODE_PRIVATE);
linux下一個文件的權(quán)限由10位標(biāo)示:
1位:文件的類型,d:文件夾 l:快捷方式 -:文件
2-4: 該文件所屬用戶對本文件的權(quán)限 , rwx :用二進(jìn)制標(biāo)示,如果不是-就用1標(biāo)示,是-用0標(biāo)示;chmod指令賦權(quán)限。
5-7:該文件所屬用戶組對本文件的權(quán)限
8-10:其他用戶對該文件的權(quán)限。
8.SharedPreferences介紹 (重點(diǎn)) 用來做數(shù)據(jù)存儲
sharedPreferences是通過xml文件來做數(shù)據(jù)存儲的。
一般用來存放一些標(biāo)記性的數(shù)據(jù),一些設(shè)置信息。
*********使用sharedPreferences存儲數(shù)據(jù)
1.通過Context對象創(chuàng)建一個SharedPreference對象
//name:sharedpreference文件的名稱 mode:文件的操作模式
SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
2.通過sharedPreferences對象獲取一個Editor對象
Editor editor = sharedPreferences.edit();
3.往Editor中添加數(shù)據(jù)
editor.putString("username", username);
editor.putString("password", password);
4.提交Editor對象
editor.commit();
*********使用sharedPreferences讀取數(shù)據(jù)
1.通過Context對象創(chuàng)建一個SharedPreference對象
SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
2.通過sharedPreference獲取存放的數(shù)據(jù)
//key:存放數(shù)據(jù)時的key defValue: 默認(rèn)值,根據(jù)業(yè)務(wù)需求來寫
String username = sharedPreferences.getString("username", "");
String password = sharedPreferences.getString("password", "");
通過PreferenceManager可以獲取一個默認(rèn)的sharepreferences對象
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
9 生成xml的2種方式
1.寫布局
2.業(yè)務(wù)邏輯
a.備份
1.封裝短信數(shù)據(jù)到list中
2.將list中的數(shù)據(jù)寫到xml文件中。
b.恢復(fù)
1.解析xml文件中短信數(shù)據(jù),封裝到list集合中
2.將解析數(shù)據(jù)打印。
XmlSerializer
//使用XmlSerializer來序列化xml文件
public static boolean backupSms_android(Context context){
try{
//0.獲取短信數(shù)據(jù)
ArrayList<SmsBean> allSms = SmsDao.getAllSms();
//1.通過Xml獲取一個XmlSerializer對象
XmlSerializer xs = Xml.newSerializer();
//2.設(shè)置XmlSerializer的一些參數(shù),比如:設(shè)置xml寫入到哪個文件中
//os:xml文件寫入流 encoding:流的編碼
xs.setOutput(context.openFileOutput("backupsms2.xml", Context.MODE_PRIVATE), "utf-8");
//3.序列化一個xml的聲明頭
//encoding:xml文件的編碼 standalone:是否獨(dú)立
xs.startDocument("utf-8", true);
//4.序列化一個根節(jié)點(diǎn)的開始節(jié)點(diǎn)
//namespace:命名空間 name: 標(biāo)簽的名稱
xs.startTag(null, "Smss");
//5.循環(huán)遍歷list集合序列化一條條短信
for (SmsBean smsBean : allSms) {
xs.startTag(null, "Sms");
//name:屬性的名稱 value:屬性值
xs.attribute(null, "id", smsBean.id+"");
xs.startTag(null, "num");
//寫一個標(biāo)簽的內(nèi)容
xs.text(smsBean.num);
xs.endTag(null, "num");
xs.startTag(null, "msg");
xs.text(smsBean.msg);
xs.endTag(null, "msg");
xs.startTag(null, "date");
xs.text(smsBean.date);
xs.endTag(null, "date");
xs.endTag(null, "Sms");
}
//6.序列化一個根節(jié)點(diǎn)的結(jié)束節(jié)點(diǎn)
xs.endTag(null, "Smss");
//7.將xml寫入到文件中,完成xml的序列化
xs.endDocument();
return true;
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
10.使用pull解析xml格式的數(shù)據(jù)
dom解析:基于全文加載的解析方式 sax解析:基于事件的逐行解析方式 pull解析:同sax
XmlPullParser
//解析xml文件讀取短信內(nèi)容
public static int restoreSms(Context context) {
ArrayList<SmsBean> arrayList = null;
SmsBean smsBean = null;
try{
//1.通過Xml獲取一個XmlPullParser對象
XmlPullParser xpp = Xml.newPullParser();
//2.設(shè)置XmlPullParser對象的參數(shù),需要解析的是哪個xml文件,設(shè)置一個文件讀取流
//通過context獲取一個資產(chǎn)管理者對象
AssetManager assets = context.getAssets();
//通過資產(chǎn)管理者對象能獲取一個文件讀取流
InputStream inputStream = assets.open("backupsms.xml");
xpp.setInput(inputStream,"utf-8");
//xpp.setInput(context.openFileInput("backupsms2.xml"), "utf-8");
//3.獲取當(dāng)前xml行的事件類型
int type = xpp.getEventType();
//4.判斷事件類型是否是文檔結(jié)束的事件類型
while(type != XmlPullParser.END_DOCUMENT){
//5.如果不是,循環(huán)遍歷解析每一行的數(shù)據(jù)。解析一行后,獲取下一行的事件類型
String currentTagName = xpp.getName();
//判斷當(dāng)前行的事件類型是開始標(biāo)簽還是結(jié)束標(biāo)簽
switch (type) {
case XmlPullParser.START_TAG:
if(currentTagName.equals("Smss")){
//如果當(dāng)前標(biāo)簽是Smss,需要初始化一個集合
arrayList = new ArrayList<SmsBean>();
}else if(currentTagName.equals("Sms")){
smsBean = new SmsBean();
smsBean.id = Integer.valueOf(xpp.getAttributeValue(null, "id"));
}else if(currentTagName.equals("num")){
smsBean.num = xpp.nextText();
}else if(currentTagName.equals("msg")){
smsBean.msg = xpp.nextText();
}else if(currentTagName.equals("date")){
smsBean.date = xpp.nextText();
}
break;
case XmlPullParser.END_TAG:
//當(dāng)前結(jié)束標(biāo)簽是Sms的話,一條短信數(shù)據(jù)封裝完成, 可以加入list中
if(currentTagName.equals("Sms")){
arrayList.add(smsBean);
}
break;
default:
break;
}
type = xpp.next();//獲取下一行的事件類型
}
return arrayList.size();
}catch (Exception e) {
e.printStackTrace();
}
return 0;
}