package com.data;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
public class Data{
//保存WIFI信息
private static String id = "saveinfo";
public static String theLast ="thelast";
public static int flag = 2;
public static String path = "/sleep/";
public static boolean play = true;
public static boolean virbrate = true;
public static String play_time = "play_time";
public static String play_name = "play_name";
public static String clock_time = "clock_time";
public static int dTime = 10;
public static int count=0;
public static void writeShare(Context context,int mode,String name,String content){
//Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,
//只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
//Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
SharedPreferences mySharedPreferences ;
switch(mode){
case 0:mySharedPreferences= context.getSharedPreferences(id, Activity.MODE_PRIVATE);break;
case 1:mySharedPreferences= context.getSharedPreferences(id, Activity.MODE_APPEND);break;
default:mySharedPreferences= context.getSharedPreferences(id, Activity.MODE_PRIVATE);break;
}
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString(name, content);
editor.commit();
}
public static String readShare(Context context,int mode,String name){
String content;
SharedPreferences mySharedPreferences ;
switch(mode){
case 0:mySharedPreferences= context.getSharedPreferences(id, Activity.MODE_PRIVATE);break;
case 1:mySharedPreferences= context.getSharedPreferences(id, Activity.MODE_APPEND);break;
default:mySharedPreferences= context.getSharedPreferences(id, Activity.MODE_PRIVATE);break;
}
content=mySharedPreferences.getString(name, "");
return content;
}
//得到内置sd卡目录
public static String getSdDir(){
String str;
File sdfile = Environment.getExternalStorageDirectory();
str=sdfile.toString();
return str;
}
//得到根目录
public static String getRootPath(String mypath){
String root=getSdDir()+mypath;
File path=new File(root);
if(!path.exists()){
path.mkdirs();
}
return root;
}
public static List<String> getFileDir(String filePath) {
ArrayList<String> list = null;
try{
File f = new File(filePath);
File[] files = f.listFiles();// 列出所有文件
// 如果不是根目录,则列出返回根目录和上一目录选项
// 将所有文件存入list中
if(files != null){
list = new ArrayList<String>();
int count = files.length;// 文件个数
for (int i = 0; i < count; i++) {
File file = files[i];
list.add(file.getPath());
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return list;
}
public static List<String> getFileName(String filePath) {
ArrayList<String> list = null;
try{
File f = new File(filePath);
File[] files = f.listFiles();// 列出所有文件
// 如果不是根目录,则列出返回根目录和上一目录选项
// 将所有文件存入list中
if(files != null){
list = new ArrayList<String>();
int count = files.length;// 文件个数
for (int i = 0; i < count; i++) {
File file = files[i];
list.add(file.getName());
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return list;
}
}
|
|