一、ProgressDialog(是一个含有进度条以及消息提示的对话框)
ProgressDialog的使用:
1、创建对象;
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
2、调用对象相应方法来完成信息的配置;
dialog.setTitle("软件更新");dialog.setMax(100);dialog.setMessage("软件正在更新...");dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
3、设置事件的监听;
-
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();
4、.show();
dialog.show();
二、数据存储
1、内部存储(手机中特定的存储区域,在这块区域中主要存储的是手机安装程序、系统文件)
(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(); -
}
2、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();
3、SD卡文件的响应操作
(1)检测是否挂载
-
/** -
* 检测dCard是否已经挂载 -
* [@return](http://my.oschina.net/u/556800) -
*/ -
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();}}}}