BlackNumberOpenHelper.java
package com.itheima52.mobilesafe.db.dao; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class BlackNumberOpenHelper extends SQLiteOpenHelper{ public BlackNumberOpenHelper(Context context) { super(context, "safe.db", null, 1); } /** * blacknumber 表名 * _id 主键自动增长 * number 电话号码 * mode 拦截模式 电话拦截 短信拦截 */ @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) { } }
BlackNumberDao.java
package main.java.com.itheima52.mobilesafe.db.dao; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.SystemClock; import com.itheima52.mobilesafe.bean.BlackNumberInfo; import java.util.ArrayList; import java.util.List; 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; } } /** * 通过电话号码修改拦截的模式 */ 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});//"number=?"是where条件 if (rownumber == 0) { return false; } else { return true; } } /** * 返回一个黑名单号码拦截模式 */ 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; } /** * 查询所有的黑名单 */ 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(); 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 每页展示的最大的条目 */ 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; } /** * 获取总的记录数 */ 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; } }
测试TestBlackNumberDao.java
package com.itheima52.mobilesafe; import android.content.Context; import android.test.AndroidTestCase; import com.itheima52.mobilesafe.bean.BlackNumberInfo; import com.itheima52.mobilesafe.db.dao.BlackNumberDao; import java.util.List; import java.util.Random; 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()); } } }
时间: 2024-12-24 12:49:19