手机安全卫士学习3--黑名单的拦截

1创建一个数据库的帮助类,并重写其构造方法

package com.djf.mobilesafty.blacknumber.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.nfc.NfcAdapter.CreateBeamUrisCallback;

public class BlackNumberOpenDBhelper extends SQLiteOpenHelper {

    public BlackNumberOpenDBhelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, "blacknumber.db", null, 1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("create table blacknumber (_id integer primary key autoincrement, number verchar(20),mode verchar(2))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

 

2 创建数据库的增删改查方法

package com.djf.mobilesafty.blacknumber.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.djf.mobilesafty.blacknumber.db.BlackNumberOpenDBhelper;
import com.djf.mobilesafty.blacknumber.domin.NumberInfo;

public class BlackNumberDao {
    private BlackNumberOpenDBhelper helper;

    /**
     * 构造方法
     */
    public BlackNumberDao(Context context) {
        helper = new BlackNumberOpenDBhelper(context, "blacknumber.db", null, 1);
    }

    /***
     * 查询黑名单是否存在
     */
    public boolean findnumber(String number) {
        boolean result = false;
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery(
                "select * from blacknumber where number=? ",
                new String[] { number });
        if (cursor.moveToNext()) {
            result = true;
        }
        cursor.close();
        db.close();
        return result;
    }
    /***
     * 查询所有黑名单
     */
    public List<NumberInfo> findall(){
        List<NumberInfo> info = new ArrayList<NumberInfo>();
        SQLiteDatabase db  =helper.getReadableDatabase();
        Cursor cursor =db.rawQuery("select number ,mode from blacknumber", null);
        while (cursor.moveToNext()) {
            NumberInfo numberInfo = new NumberInfo();
            String number = cursor.getString(0);
            String mode = cursor.getString(1);
            numberInfo.setNumber(number);
            numberInfo.setMode(mode);
            info.add(numberInfo);
        }
        cursor.close();
        db.close();
        return info;
    }

    /**
     * 增加黑名单
     * mode 1 短信拦截 2 电话拦截3 全部拦截
     */
    public void add(String number, String mode) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("number", number);
        cv.put("mode", mode);
        db.insert("blacknumber", null, cv);
        db.close();
    }
    /**
     * 增加黑名单
     * 修改拦截模式
     */
    public void update(String number, String mode) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        
        cv.put("mode", mode);
        db.update("blacknumber", cv, "number=?", new String[]{number});
        db.close();
    }
    /**
     * 删除黑名单
     * 修改拦截模式
     */
    public void delete(String number) {
        SQLiteDatabase db = helper.getWritableDatabase();
    
        db.delete("blacknumber", "number=?", new String[]{number});
    
        db.close();
    }
}

 

3创建相关信息的业务类

package com.djf.mobilesafty.blacknumber.domin;

public class NumberInfo {
private String number;
private String mode;
public String getNumber() {
    return number;
}
public void setNumber(String number) {
    this.number = number;
}
public String getMode() {
    return mode;
}
public void setMode(String mode) {
    this.mode = mode;
}
@Override
public String toString() {
    return "numberinfor [number=" + number + ", mode=" + mode + "]";
}


}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。