1、ProgressDialog(是一個含有進度條以及消息提示的對話框)java
ProgressDialog的使用:ide
一、建立對象;this
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
二、調用對象相應方法來完成信息的配置;spa
dialog.setTitle("軟件更新");
dialog.setMax(100);
dialog.setMessage("軟件正在更新...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
三、設置事件的監聽;.net
dialog.setButton3("肯定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setButton2("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
num=0;
flag=false;
dialog.cancel();
dialog.dismiss();
}
});
new Thread(new Runnable() {
@Override
public void run() {
while (num <= 100 || flag==true) {
try {
Thread.sleep(100);
dialog.setProgress(num);
num++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
四、.show();code
dialog.show();
2、數據存儲xml
一、內部存儲(手機中特定的存儲區域,在這塊區域中主要存儲的是手機安裝程序、系統文件)對象
(1)內部存儲區域中的文件的特色是:能夠給圖片設置一個權限,這個權限通常狀況下都是隻容許當前的應用來訪問這個文件。事件
(2)內部存儲區域的訪問:圖片
<1>文件的建立
/**
* 建立一個文件
*/
private void createFile() {
File file = getFilesDir();
File file2=new File(file,"a.txt");
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<2>向文件中寫入內容
/**
* 向文件中寫入內容
*/
private void writeToFile() {
File file = getFilesDir();
File file2=new File(file,"a.txt");
FileOutputStream out=null;
try {
out=new FileOutputStream(file2);
out.write("我是中國人".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (out!=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
<3>讀取文件中的數據
/**
* 讀取文件中的內容
*/
private void readFromFile() {
File file=getFilesDir();
File file2=new File(file, "a.txt");
FileInputStream input=null;
try {
input=new FileInputStream(file2);
byte[] buf=new byte[(int) file2.length()];
input.read(buf);
String result=new String(buf);
Log.i("--文件中的信息---", result);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (input!=null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
<4>建立文件夾
/**
* 建立文件夾
*/
private void createDir() {
/*
* 第一種方式
*/
/*File file=getDir("b.txt", Context.MODE_PRIVATE);
if (!file.exists()) {
file.mkdir();
}*/
/*
* 第二種方式
*/
String path=getFilesDir().getAbsolutePath()+"/c.txt";
new File(path).mkdir();
}
<5>刪除目錄或者文件
case R.id.btn_06:
String path=getFilesDir().getAbsolutePath()+"/c.txt";
File file=new File(path);
deleteFileOrDir(file);
break;
/**
* 刪除目錄或文件
*/
private void deleteFileOrDir(File file) {
if (file.isFile()) {
file.delete();
}else if (file.isDirectory()) {
File[] files = file.listFiles();
if (files!=null) {
for (int i = 0; i < files.length; i++) {
deleteFileOrDir(files[i]);
}
}
}
file.delete();
}
二、SharedPreferences:
通常狀況下SharedPreferences的用途:
1.放置用戶的登陸信息;
2.放置軟件的配置信息;
3.放置臨時數據;
特色:數據的存儲採用的是鍵值對的形式來進行存儲的,鍵是不能夠重複的,值是能夠重複的(相似Map)
(1)SharedPreferences的使用步驟:
1.獲取SharedPreferences的對象;
第一個參數:表示SharedPreferences中的XML文件的名稱,不須要添加.xml;
第二個參數:表示建立的文件的訪問權限,通常狀況下寫成Context.MODE_PRIVATE(表示只可以本身訪問)
SharedPreferences sp = getSharedPreferences("abcd", Context.MODE_PRIVATE);
2.添加數據須要經過sp.edit()來獲取一個Eidtor對象;
Editor edit = sp.edit();
3.經過Eidtor對象的put方法向裏面添加數據;
edit.putBoolean("key1", true);
edit.putInt("num", 100);
edit.putString("name", "xiaoming");
4.添加完數據必須經過.commit()方法提交數據,必須執行此方法。
edit.commit();
(2)SharedPreferences的訪問步驟:
1.獲取SharedPreferences的對象;
第一個參數:表示SharedPreferences中的XML文件的名稱,不須要添加.xml;
第二個參數:表示建立的文件的訪問權限,通常狀況下寫成Context.MODE_PRIVATE(表示只可以本身訪問)
SharedPreferences sp1 = getSharedPreferences("abcd", Context.MODE_PRIVATE);
2.經過SharedPreferences的get方法來獲取數據
boolean b=sp1.getBoolean("key1", false);
int i=sp1.getInt("num", 0);
String string=sp1.getString("name", "小明");
Log.i("---------", b+" "+i+" "+string);
(3)SharedPreferences的清除步驟:
1. 1.獲取SharedPreferences的對象;
第一個參數:表示SharedPreferences中的XML文件的名稱,不須要添加.xml;
第二個參數:表示建立的文件的訪問權限,通常狀況下寫成Context.MODE_PRIVATE(表示只可以本身訪問)
SharedPreferences sp2 = getSharedPreferences("abcd", Context.MODE_PRIVATE);
2.獲取Eidt對象
Editor edit2 = sp2.edit();
3.經過edit2.remove("num")或edit2.clear()來進行刪除操做
edit2.remove("num");
edit2.clear();
4.提交請求
edit2.commit();
三、SD卡文件的響應操做
(1)檢測是否掛載
/**
* 檢測dCard是否已經掛載
* @return
*/
public static boolean checkSdcardExistOrNot(){
//返回的是當前SdCard的狀態
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}else {
return false;
}
}
(2)獲取當前的SdCard的剩餘空間大小
/**
* 獲取當前的SdCard的剩餘空間的大小
*/
@SuppressWarnings("deprecation")
public static int getFreeSpaceSize(){
//第一步:獲取的是SdCard的根目錄
File file = Environment.getExternalStorageDirectory();
//第二步:獲取Statfs(專門用來獲取系統信息)
StatFs sf=new StatFs(file.getAbsolutePath());
//獲取每個塊的大小
int blockSize = sf.getBlockSize();
//獲取一共有多少個塊
int blockCount = sf.getBlockCount();
//獲取空閒的快的數量
int availableBlocks = sf.getAvailableBlocks();
Log.i("SdCard的總大小", -blockCount*blockSize/1024/1024+"MB");
Log.i("SdCard的總大小", -availableBlocks*blockSize/1024/1024+"MB");
return availableBlocks*blockSize/1024/1024;
}
(3)複製文件
/**
* 把前面的文件複製到後面的文件夾中
*/
public static void copyFile(File fileSource,File fileDesternation){
FileInputStream input=null;
FileOutputStream out=null;
try {
input=new FileInputStream(fileSource);
out=new FileOutputStream(fileDesternation);
byte [] buf=new byte[1024];
int len;
while ((len=input.read(buf))!=-1) {
out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (input!=null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out!=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}