Android第四十五天

Stella981
• 阅读 411

一、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. }
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
2年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
2个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这