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. }
相关文章
相关标签/搜索