标签:
1.创建帮助类,创建表
public class BlackNumberOpenHelper extends SQLiteOpenHelper{
public BlackNumberOpenHelper(Context context) {
super(context, "safe.db", null, 1);
}
/**
* blacknumber 表名
* _id 主键自动增长
* number 电话号码
* mode 拦截模式 电话拦截 短信拦截
* @param db
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table blacknumber (_id integer primary key autoincrement,number varchar(20),mode varchar(2))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class BlackNumberDao {
public BlackNumberOpenHelper helper;
public BlackNumberDao(Context context) {
helper = new BlackNumberOpenHelper(context);
}
/**
* @param number 黑名单号码
* @param mode 拦截模式
*/
public boolean add(String number, String mode) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("number", number);
contentValues.put("mode", mode);
long rowid = db.insert("blacknumber", null, contentValues);
if (rowid == -1) {
return false;
} else {
return true;
}
}
/**
* 通过电话号码删除
*
* @param number 电话号码
*/
public boolean delete(String number) {
SQLiteDatabase db = helper.getWritableDatabase();
int rowNumber = db.delete("blacknumber", "number=?", new String[]{number});
if (rowNumber == 0) {
return false;
} else {
return true;
}
}
/**
* 通过电话号码修改拦截的模式
*
* @param number
*/
public boolean changeNumberMode(String number, String mode) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("mode", mode);
int rownumber =db.update("blacknumber", values, "number=?", new String[]{number});
if (rownumber == 0) {
return false;
} else {
return true;
}
}
/**
* 返回一个黑名单号码拦截模式
*
* @return
*/
public String findNumber(String number) {
String mode = "";
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query("blacknumber", new String[]{"mode"}, "number=?", new String[]{number}, null, null, null);
if (cursor.moveToNext()) {
mode = cursor.getString(0);
}
cursor.close();
db.close();
return mode;
}
/**
* 查询所有的黑名单
*
* @return
*/
public List<BlackNumberInfo> findAll() {
SQLiteDatabase db = helper.getReadableDatabase();
List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
Cursor cursor = db.query("blacknumber", new String[]{"number", "mode"}, null, null, null, null, null);
while (cursor.moveToNext()) {
BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
blackNumberInfo.setNumber(cursor.getString(0));
blackNumberInfo.setMode(cursor.getString(1));
blackNumberInfos.add(blackNumberInfo);
}
cursor.close();
db.close();
SystemClock.sleep(3000);
return blackNumberInfos;
}
/**
* 分页加载数据
*
* @param pageNumber 表示当前是哪一页
* @param pageSize 表示每一页有多少条数据
* @return limit 表示限制当前有多少数据
* offset 表示跳过 从第几条开始
*/
public List<BlackNumberInfo> findPar(int pageNumber, int pageSize) {
SQLiteDatabase db = helper.getReadableDatabase();
// 数值转为字符串:string.valueof()
// 也可以字符串转为数值:Folat.valueof(str).folatValue() Double.valueof(str).doubleValue()
Cursor cursor = db.rawQuery("select number,mode from blacknumber limit ? offset ?", new String[]{String.valueOf(pageSize),
String.valueOf(pageSize * pageNumber)});
List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
while (cursor.moveToNext()) {
BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
blackNumberInfo.setMode(cursor.getString(1));
blackNumberInfo.setNumber(cursor.getString(0));
blackNumberInfos.add(blackNumberInfo);
}
cursor.close();
db.close();
return blackNumberInfos;
}
/**
* 分批加载数据
* @param startIndex 开始的位置
* @param maxCount 每页展示的最大的条目
* @return
*/
public List<BlackNumberInfo> findPar2(int startIndex, int maxCount) {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select number,mode from blacknumber limit ? offset ?", new String[]{String.valueOf(maxCount),
String.valueOf(startIndex)});
List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
while (cursor.moveToNext()) {
BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
blackNumberInfo.setMode(cursor.getString(1));
blackNumberInfo.setNumber(cursor.getString(0));
blackNumberInfos.add(blackNumberInfo);
}
cursor.close();
db.close();
return blackNumberInfos;
}
/**
* 获取总的记录数
* @return
*/
public int getTotalNumber(){
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from blacknumber", null);
cursor.moveToNext();
int count = cursor.getInt(0);
cursor.close();
db.close();
return count;
}
}
public class TestBlackNumberDao extends AndroidTestCase {
public Context mContext;
@Override
protected void setUp() throws Exception {
this.mContext = getContext();
super.setUp();
}
public void testAdd(){
BlackNumberDao dao = new BlackNumberDao(mContext);
Random random = new Random();
for (int i = 0; i < 200; i++) {
Long number = 13300000000l +i;
dao.add(number +"",String.valueOf(random.nextInt(3) + 1));
}
}
public void testDelete(){
BlackNumberDao dao = new BlackNumberDao(mContext);
boolean delete = dao.delete("13300000000");
assertEquals(true,delete);
}
public void testFind(){
BlackNumberDao dao = new BlackNumberDao(mContext);
String number = dao.findNumber("13300000004");
System.out.println(number);
}
public void testFindAll(){
BlackNumberDao dao = new BlackNumberDao(mContext);
List<BlackNumberInfo> blackNumberInfos = dao.findAll();
for (BlackNumberInfo blackNumberInfo : blackNumberInfos) {
System.out.println(blackNumberInfo.getMode() + "" + blackNumberInfo.getNumber());
}
}
}
标签:
原文地址:http://www.cnblogs.com/liuyu0529/p/4923684.html