Android第四十五天 4年前

移动应用
2768
Android第四十五天

一、ProgressDialog(是一个含有进度条以及消息提示的对话框)

        ProgressDialog的使用:

                1、创建对象;

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

                2、调用对象相应方法来完成信息的配置;

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

                3、设置事件的监听;

  1. dialog.setButton3("确定", new DialogInterface.OnClickListener() {

  2. @Override

  3. public void onClick(DialogInterface dialog, int which) {

  4. }

  5. });

  6. dialog.setButton2("取消", new DialogInterface.OnClickListener() {

  7. @Override

  8. public void onClick(DialogInterface dialog, int which) {

  9. num=0;

  10. flag=false;

  11. dialog.cancel();

  12. dialog.dismiss();

  13. }

  14. });

  15. new Thread(new Runnable() {

  16. @Override

  17. public void run() {

  18. while (num <= 100 || flag==true) {

  19. try {

  20. Thread.sleep(100);

  21. dialog.setProgress(num);

  22. num++;

  23. } catch (InterruptedException e) {

  24. e.printStackTrace();

  25. }

  26. }

  27. }

  28. }).start();

                4、.show();

  1. dialog.show();

二、数据存储

        1、内部存储(手机中特定的存储区域,在这块区域中主要存储的是手机安装程序、系统文件)

                (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. private void createDir() {

  5. /*

  6. * 第一种方式

  7. */

  8. /*File file=getDir("b.txt", Context.MODE_PRIVATE);

  9. if (!file.exists()) {

  10. file.mkdir();

  11. }*/

  12. /*

  13. * 第二种方式

  14. */

  15. String path=getFilesDir().getAbsolutePath()+"/c.txt";

  16. new File(path).mkdir();

  17. }

                        <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;

  6. /**

  7. * 删除目录或文件

  8. */

  9. private void deleteFileOrDir(File file) {

  10. if (file.isFile()) {

  11. file.delete();

  12. }else if (file.isDirectory()) {

  13. File[] files = file.listFiles();

  14. if (files!=null) {

  15. for (int i = 0; i < files.length; i++) {

  16. deleteFileOrDir(files[i]);

  17. }

  18. }

  19. }

  20. file.delete();

  21. }

        2、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();

        3、SD卡文件的响应操作

                (1)检测是否挂载

  1. /**

  2. * 检测dCard是否已经挂载

  3. * [@return](http://my.oschina.net/u/556800)

  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. }

                (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. }

                (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. }
高达战士
你说夜里做的决定隔天就会反悔,那么我就要为了你的反悔等到零点。
3
发布数
4
关注者
3902
累计阅读

热门教程文档

QT
33小节
Next
43小节
Djiango
17小节
Lua
21小节
Kotlin
68小节
广告