Android第四十五天

1、ProgressDialog(是一個含有進度條以及消息提示的對話框)java

        ProgressDialog的使用:ide

                一、建立對象;this

 
  1. final ProgressDialog dialog = new ProgressDialog(MainActivity.this);

                二、調用對象相應方法來完成信息的配置;spa

 
  1. dialog.setTitle("軟件更新");
  2. dialog.setMax(100);
  3. dialog.setMessage("軟件正在更新...");
  4. dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

                三、設置事件的監聽;.net

 
  1. dialog.setButton3("肯定", new DialogInterface.OnClickListener() {
  2.  
  3. @Override
  4. public void onClick(DialogInterface dialog, int which) {
  5.  
  6. }
  7. });
  8. dialog.setButton2("取消", new DialogInterface.OnClickListener() {
  9.  
  10. @Override
  11. public void onClick(DialogInterface dialog, int which) {
  12. num=0;
  13. flag=false;
  14. dialog.cancel();
  15. dialog.dismiss();
  16. }
  17. });
  18. new Thread(new Runnable() {
  19.  
  20. @Override
  21. public void run() {
  22. while (num <= 100 || flag==true) {
  23. try {
  24. Thread.sleep(100);
  25. dialog.setProgress(num);
  26. num++;
  27. } catch (InterruptedException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31.  
  32. }
  33. }).start();

                四、.show();code

 
  1. dialog.show();

2、數據存儲xml

        一、內部存儲(手機中特定的存儲區域,在這塊區域中主要存儲的是手機安裝程序、系統文件)對象

                (1)內部存儲區域中的文件的特色是:能夠給圖片設置一個權限,這個權限通常狀況下都是隻容許當前的應用來訪問這個文件。事件

                (2)內部存儲區域的訪問:圖片

                        <1>文件的建立

 
  1. /**
  2. * 建立一個文件
  3. */
  4. private void createFile() {
  5. File file = getFilesDir();
  6. File file2=new File(file,"a.txt");
  7. if (!file2.exists()) {
  8. try {
  9. file2.createNewFile();
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. }

                        <2>向文件中寫入內容

 
  1. /**
  2. * 向文件中寫入內容
  3. */
  4. private void writeToFile() {
  5. File file = getFilesDir();
  6. File file2=new File(file,"a.txt");
  7. FileOutputStream out=null;
  8. try {
  9. out=new FileOutputStream(file2);
  10. out.write("我是中國人".getBytes());
  11. } catch (FileNotFoundException e) {
  12. e.printStackTrace();
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }finally{
  16. if (out!=null) {
  17. try {
  18. out.close();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }
  24. }

                        <3>讀取文件中的數據

 
  1. /**
  2. * 讀取文件中的內容
  3. */
  4. private void readFromFile() {
  5. File file=getFilesDir();
  6. File file2=new File(file, "a.txt");
  7. FileInputStream input=null;
  8. try {
  9. input=new FileInputStream(file2);
  10. byte[] buf=new byte[(int) file2.length()];
  11. input.read(buf);
  12. String result=new String(buf);
  13. Log.i("--文件中的信息---", result);
  14. } catch (FileNotFoundException e) {
  15. e.printStackTrace();
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }finally{
  19. if (input!=null) {
  20. try {
  21. input.close();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27. }

                        <4>建立文件夾

 
  1. /**
  2. * 建立文件夾
  3. */
  4.  
  5. private void createDir() {
  6. /*
  7. * 第一種方式
  8. */
  9. /*File file=getDir("b.txt", Context.MODE_PRIVATE);
  10. if (!file.exists()) {
  11. file.mkdir();
  12. }*/
  13. /*
  14. * 第二種方式
  15. */
  16. String path=getFilesDir().getAbsolutePath()+"/c.txt";
  17. new File(path).mkdir();
  18. }

                        <5>刪除目錄或者文件

 
  1. case R.id.btn_06:
  2. String path=getFilesDir().getAbsolutePath()+"/c.txt";
  3. File file=new File(path);
  4. deleteFileOrDir(file);
  5. break;
 
  1. /**
  2. * 刪除目錄或文件
  3. */
  4. private void deleteFileOrDir(File file) {
  5. if (file.isFile()) {
  6. file.delete();
  7. }else if (file.isDirectory()) {
  8. File[] files = file.listFiles();
  9. if (files!=null) {
  10. for (int i = 0; i < files.length; i++) {
  11. deleteFileOrDir(files[i]);
  12. }
  13. }
  14. }
  15. file.delete();
  16. }

        二、SharedPreferences:

                通常狀況下SharedPreferences的用途:

                        1.放置用戶的登陸信息;

                        2.放置軟件的配置信息;

                        3.放置臨時數據;

                特色:數據的存儲採用的是鍵值對的形式來進行存儲的,鍵是不能夠重複的,值是能夠重複的(相似Map)

                (1)SharedPreferences的使用步驟:

                        1.獲取SharedPreferences的對象;

                                第一個參數:表示SharedPreferences中的XML文件的名稱,不須要添加.xml;

                                第二個參數:表示建立的文件的訪問權限,通常狀況下寫成Context.MODE_PRIVATE(表示只可以本身訪問)

 
  1. SharedPreferences sp = getSharedPreferences("abcd", Context.MODE_PRIVATE);

                        2.添加數據須要經過sp.edit()來獲取一個Eidtor對象;

 
  1. Editor edit = sp.edit();

                        3.經過Eidtor對象的put方法向裏面添加數據;

 
  1. edit.putBoolean("key1", true);
  2. edit.putInt("num", 100);
  3. edit.putString("name", "xiaoming");

                        4.添加完數據必須經過.commit()方法提交數據,必須執行此方法。

 
  1. edit.commit();

                (2)SharedPreferences的訪問步驟:

                        1.獲取SharedPreferences的對象;

                                第一個參數:表示SharedPreferences中的XML文件的名稱,不須要添加.xml;

                                第二個參數:表示建立的文件的訪問權限,通常狀況下寫成Context.MODE_PRIVATE(表示只可以本身訪問)

 
  1. SharedPreferences sp1 = getSharedPreferences("abcd", Context.MODE_PRIVATE);

                        2.經過SharedPreferences的get方法來獲取數據

 
  1. boolean b=sp1.getBoolean("key1", false);
  2. int i=sp1.getInt("num", 0);
  3. String string=sp1.getString("name", "小明");
  4. Log.i("---------", b+" "+i+" "+string);

                (3)SharedPreferences的清除步驟:

                        1.  1.獲取SharedPreferences的對象;

                                第一個參數:表示SharedPreferences中的XML文件的名稱,不須要添加.xml;

                                第二個參數:表示建立的文件的訪問權限,通常狀況下寫成Context.MODE_PRIVATE(表示只可以本身訪問)

 
  1. SharedPreferences sp2 = getSharedPreferences("abcd", Context.MODE_PRIVATE);

                        2.獲取Eidt對象

 
  1. Editor edit2 = sp2.edit();

                        3.經過edit2.remove("num")或edit2.clear()來進行刪除操做

 
  1. edit2.remove("num");
  2. edit2.clear();

                        4.提交請求

 
  1. edit2.commit();

        三、SD卡文件的響應操做

                (1)檢測是否掛載

 
  1. /**
  2. * 檢測dCard是否已經掛載
  3. * @return
  4. */
  5. public static boolean checkSdcardExistOrNot(){
  6. //返回的是當前SdCard的狀態
  7. String state = Environment.getExternalStorageState();
  8. if (Environment.MEDIA_MOUNTED.equals(state)) {
  9. return true;
  10. }else {
  11. return false;
  12. }
  13.  
  14. }

                (2)獲取當前的SdCard的剩餘空間大小

 
  1. /**
  2. * 獲取當前的SdCard的剩餘空間的大小
  3. */
  4. @SuppressWarnings("deprecation")
  5. public static int getFreeSpaceSize(){
  6. //第一步:獲取的是SdCard的根目錄
  7. File file = Environment.getExternalStorageDirectory();
  8. //第二步:獲取Statfs(專門用來獲取系統信息)
  9. StatFs sf=new StatFs(file.getAbsolutePath());
  10. //獲取每個塊的大小
  11. int blockSize = sf.getBlockSize();
  12. //獲取一共有多少個塊
  13. int blockCount = sf.getBlockCount();
  14. //獲取空閒的快的數量
  15. int availableBlocks = sf.getAvailableBlocks();
  16. Log.i("SdCard的總大小", -blockCount*blockSize/1024/1024+"MB");
  17. Log.i("SdCard的總大小", -availableBlocks*blockSize/1024/1024+"MB");
  18. return availableBlocks*blockSize/1024/1024;
  19.  
  20. }

                (3)複製文件

 
  1. /**
  2. * 把前面的文件複製到後面的文件夾中
  3. */
  4. public static void copyFile(File fileSource,File fileDesternation){
  5. FileInputStream input=null;
  6. FileOutputStream out=null;
  7. try {
  8. input=new FileInputStream(fileSource);
  9. out=new FileOutputStream(fileDesternation);
  10. byte [] buf=new byte[1024];
  11. int len;
  12. while ((len=input.read(buf))!=-1) {
  13. out.write(buf, 0, len);
  14. }
  15. } catch (FileNotFoundException e) {
  16. e.printStackTrace();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }finally{
  20. if (input!=null) {
  21. try {
  22. input.close();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. if (out!=null) {
  28. try {
  29. out.close();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  35. }
相關文章
相關標籤/搜索