标签:mon int 空间 工具 sdc boolean esc key dex
1.SharedPreferencesUtils 引导页一般用来保存打开记录
package myapplication.com.myapp.Utils; import android.content.Context; import android.content.SharedPreferences; /** * Created by Administrator on 2016/8/9. */ public class SharedPreferencesUtils { private static String NAME="share_data"; public static void setParam(Context context, String key, Object object){ SharedPreferences p=context.getSharedPreferences(NAME,Context.MODE_APPEND); String type=object.getClass().getSimpleName(); SharedPreferences.Editor editor=p.edit(); if(type.equals("String")){ editor.putString(key,(String)object); } else if(type.equals("Integer")){ editor.putInt(key,(Integer) object); } else if(type.equals("Boolean")){ editor.putBoolean(key,(Boolean) object); }else if(type.equals("Long")){ editor.putLong(key,(Long)object); } editor.commit(); } public static Object getParam(Context context,String key,Object object){ SharedPreferences p=context.getSharedPreferences(NAME,Context.MODE_APPEND); String type=object.getClass().getSimpleName(); if(type.equals("String")){ return p.getString(key,(String)object); } else if(type.equals("Integer")){ return p.getInt(key,(Integer)object); } else if (type.equals("Boolean")) { return p.getBoolean(key, (Boolean) object); } else if (type.equals("Long")) { return p.getLong(key, (Long) object); } return null; } // SharePreferenceUtils.setParam(getApplicationContext(), "is_first", true);// // boolean is_first= (boolean) SharePreferenceUtils.getParam(getApplicationContext(),"is_first",false); }
2. SDcardHelper
package myapplication.com.myapp.Utils; import android.os.Environment; import android.os.StatFs; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class SDcardHelper { /** * * * 判断sdcard是否挂载,可用 * */ public static boolean isSDcardMounted() { boolean state = Environment.getExternalStorageState(). equals(Environment.MEDIA_MOUNTED); return state; } /** * 获取sdcard的路径 * */ public static String getSDCardPath() { String path = Environment.getExternalStorageDirectory().getAbsolutePath(); return path; } /** * 获取sdcard的大小 * */ public static long getSDCardSize() { if (isSDcardMounted()) { // StatFs sf = new StatFs(getSDCardPath()); long count = sf.getBlockCount(); long size = sf.getBlockSize(); return count * size; } return 0; } /** * 获取sdcard的空余空间大小 */ public static long getSDCardFreeSize() { if (isSDcardMounted()) { StatFs sFs = new StatFs(getSDCardPath()); long count = sFs.getFreeBlocks(); long size = sFs.getBlockSize(); return count * size; } return 0; } /** * 获取sdcard的可利用空间大小 */ public static long getSDCardAvailableSize() { if (isSDcardMounted()) { StatFs sFs = new StatFs(getSDCardPath()); long count = sFs.getAvailableBlocks(); long size = sFs.getBlockSize(); return count * size; } return 0; } /** * 保存文件到sdcard * @param data 保存目标数组 * @param dir 保存路径 * @param filename 文件名 * @return */ public static boolean saveFileToSDCard(byte[] data, String dir,String filename) { if (isSDcardMounted()) { File filedir = new File(getSDCardPath() + File.separator + dir); if (!filedir.exists()) { filedir.mkdirs(); } if (getSDCardAvailableSize() >= data.length) { FileOutputStream fos = null; try { fos = new FileOutputStream(new File(filedir+File.separator+filename)); fos.write(data); fos.flush(); return true; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } return false; } /** * 从sdcard路径读取文件 ,返回字符数组 * */ public static byte[] readFileFromSDCard(String filepath) { if (isSDcardMounted()) { File file = new File(filepath); ByteArrayOutputStream byteArrayOutputStream = null; if (file.exists()) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length = 0; while ((length = fileInputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, length); } return byteArrayOutputStream.toByteArray(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } return null; } }
3.SqliteDbhelper 配合bean文件,dao一直使用
package myapplication.com.myapp.Utils; import android.database.sqlite.SQLiteDatabase; import java.io.File; public class SQLiteDBHelper { private static String DB_PATH = SDcardHelper.getSDCardPath(); private static String DB_NAME = "student.db"; // 数据库名称 static { SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH+File.separator+DB_NAME, null); db.execSQL("create table if not exists student(_id integer primary key autoincrement," + "name varchar(20)," + "sex varchar(4)," + "address varchar(100)," + "money integer)"); db.close(); } public static SQLiteDatabase getSqLiteDatabase() { return SQLiteDatabase.openOrCreateDatabase(DB_PATH+File.separator+DB_NAME, null); } }
package myapplication.com.myapp.bean; public class Student { int id; String name; String sex; String address; int money; // new Student(2, time, msg, reserved, 300) // time ,msg ,reserved public Student(int id, String name, String sex, String address, int money) { super(); this.id = id; this.name = name; this.sex = sex; this.address = address; this.money = money; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", address=" + address + ", money=" + money + "]"; } }
package myapplication.com.myapp.bean; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; import java.util.List; import myapplication.com.myapp.Utils.SQLiteDBHelper; public class StudentDao { public boolean insert(Student stu) { SQLiteDatabase db = SQLiteDBHelper.getSqLiteDatabase(); try { db.execSQL("insert into student (name,sex,address,money) values(?,?,?,?)", new Object[]{stu.getName(),stu.getSex(),stu.getAddress(),stu.getMoney()}); db.close(); return true; } catch (Exception e) { // TODO: handle exception } return false; } public void delete(int id) { SQLiteDatabase db = SQLiteDBHelper.getSqLiteDatabase(); //db.execSQL("delete from student where _id = ?", new Object[]{student.getId()}); db.execSQL("delete from student where _id = "+id); db.close(); } public void update(Student stu) { SQLiteDatabase db = SQLiteDBHelper.getSqLiteDatabase(); db.execSQL("update student set name = ?,sex=?,address=?,money=? where _id = ?", new Object[]{stu.getName(),stu.getSex(),stu.getAddress(),stu.getMoney(),stu.getId()}); db.close(); } public Student findById(int id) { SQLiteDatabase db = SQLiteDBHelper.getSqLiteDatabase(); Cursor cursor = db.rawQuery("select * from student where _id = ?", new String[]{id+""}); Student student = null; if (cursor.moveToNext()) { int _id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); String sex = cursor.getString(cursor.getColumnIndex("sex")); String address = cursor.getString(cursor.getColumnIndex("address")); int money = cursor.getInt(cursor.getColumnIndex("money")); student = new Student(_id, name, sex, address, money); } return student; } public List<Student> findAll() { List<Student> list = new ArrayList<Student>(); SQLiteDatabase db = SQLiteDBHelper.getSqLiteDatabase(); // db.rawQuery("select * from 表名 orderby KEY_ENDTIME",null); // // KEY_ENDTIME ASC 升序 // KEY_ENDTIME DESC 降序 //Cursor cursor = db.rawQuery("select * from student", null); Cursor cursor = db.rawQuery("select * from student order by money ASC",null); Student student = null; while (cursor.moveToNext()) { int _id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); String sex = cursor.getString(cursor.getColumnIndex("sex")); String address = cursor.getString(cursor.getColumnIndex("address")); int money = cursor.getInt(cursor.getColumnIndex("money")); student = new Student(_id, name, sex, address, money); list.add(student); } return list; } }
StudentDao studentDao = new StudentDao(); studentDao.insert(new Student(pci, cellname, lon, lat, pci));
StudentDao studentDao=new StudentDao(); List<Student> list=studentDao.findAll();
标签:mon int 空间 工具 sdc boolean esc key dex
原文地址:http://www.cnblogs.com/galibujianbusana/p/6129400.html