我的手机管家骚扰拦截 黑名单数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我的手机管家骚扰拦截 黑名单数据库相关的知识,希望对你有一定的参考价值。
将添加的黑名单存储到本地数据库:
创建一个黑名单数据库, 将存储number(电话号)和mode(模式)
1 电话 2 短信 3 全部
数据库创建:
package com.chb.myphonesave.dao; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class BlackNumberOpenHelper extends SQLiteOpenHelper{ public static final String dbName = "blacknumber.db"; public BlackNumberOpenHelper(Context context) { super(context, dbName, null, 1); } @Override public void onCreate(SQLiteDatabase db) { String sql = "create table blacknumber(_id integer primary key autoincrement, " + "number varchar(20), mode integer)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
数据库管理:增删改查
package com.chb.myphonesave.dao; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.chb.myphonesave.entity.BlackNumberInfo; public class BlackNumberDao { private BlackNumberOpenHelper bnOpenHelper; private static BlackNumberDao dbDao = null; private BlackNumberDao(Context context) { bnOpenHelper = new BlackNumberOpenHelper(context); } /** * 获取数据库管理实例 */ public static BlackNumberDao getInstance(Context context) { if(dbDao == null){ synchronized (BlackNumberDao.class) { if(dbDao == null){ dbDao = new BlackNumberDao(context); } } } return dbDao; } /** * 向黑名单数据库中添加信息 * @param number * @param mode */ public void add(String number, int mode){ SQLiteDatabase db = bnOpenHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("number", number); values.put("mode", mode); db.insert(BlackNumberOpenHelper.dbName, null, values); db.close();//关闭数据库 } /** * 将制定号码移除黑名单 * @param number * @return */ public int delete(String number){ SQLiteDatabase db = bnOpenHelper.getWritableDatabase(); int flag = 0;//影响的行数 db.delete(BlackNumberOpenHelper.dbName, " number = ? ", new String[] {number}); db.close(); return flag; } /** * * @param number * @param mode * @return */ public int update(String number, int mode){ SQLiteDatabase db = bnOpenHelper.getWritableDatabase(); int flag = 0;//影响的行数 ContentValues values = new ContentValues(); values.put("mode", mode); db.update(BlackNumberOpenHelper.dbName, values, " number = ? ", new String[] {number}); db.close(); return flag; } /** * 查询指定number的条数 * @param number * @return */ public int find(String number){ SQLiteDatabase db = bnOpenHelper.getReadableDatabase(); int flag = 0;//查询的行数 Cursor cursor = db.query(BlackNumberOpenHelper.dbName, new String[] {"number", "mode"},
" number = ? ", new String[]{number}, null, null, null); flag = cursor.getCount(); db.close(); return flag; } /** * 获取指定给number的mode * @param number * @return */ public int findMode(String number){ SQLiteDatabase db = bnOpenHelper.getReadableDatabase(); int flag = 0;//查询的行数 Cursor cursor = db.query(BlackNumberOpenHelper.dbName, new String[] {"number", "mode"}, " number = ? ", new String[]{number}, null, null, null); if(cursor.getCount() > 0){ cursor.getInt(0); } db.close(); return flag; } /** * 查询所有的黑名单信息 * @return */ public List<BlackNumberInfo> findAll(){ SQLiteDatabase db = bnOpenHelper.getReadableDatabase(); List<BlackNumberInfo> list = new ArrayList<BlackNumberInfo>(); Cursor cursor = db.query(BlackNumberOpenHelper.dbName, new String[] {"number", "mode"}, null, null, null, null, null); while(cursor.moveToNext()){ String number = cursor.getString(cursor.getColumnIndex("number")); int mode = cursor.getInt(cursor.getColumnIndex("mode")); BlackNumberInfo info = new BlackNumberInfo(number, mode); list.add(info); } db.close(); return list; } /** * 查询部分的黑名单信息 * @return */ public List<BlackNumberInfo> findPart(int startIndex){ SQLiteDatabase db = bnOpenHelper.getReadableDatabase(); List<BlackNumberInfo> list = new ArrayList<BlackNumberInfo>(); Cursor cursor = db .rawQuery( "select number,mode from blacknumber order by _id desc limit ?,20", new String[] { startIndex + "" });// 按照id逆序排列 while(cursor.moveToNext()){ String number = cursor.getString(cursor.getColumnIndex("number")); int mode = cursor.getInt(cursor.getColumnIndex("mode")); BlackNumberInfo info = new BlackNumberInfo(number, mode); list.add(info); } db.close(); return list; } /** * 获取数据总数 */ public int getTotalCount() { SQLiteDatabase db = bnOpenHelper.getWritableDatabase(); Cursor cursor = db.rawQuery("select count(*) from blacknumber", null); int total = 0; if (cursor.moveToFirst()) { total = cursor.getInt(0); } cursor.close(); db.close(); return total; } }
黑名单实体类:
package com.chb.myphonesave.entity; /** * 黑名单信息实体类 */ public class BlackNumberInfo { public String number; public int mode; public BlackNumberInfo(String number, int mode) { this.number = number; this.mode = mode; } }
以上是关于我的手机管家骚扰拦截 黑名单数据库的主要内容,如果未能解决你的问题,请参考以下文章