在HomeActivity中添加进入黑名单拦截的界面的代码:
case 1://进入黑名单拦截界面
Intent intent1 = new Intent(HomeActivity.this, CallSmsSaveActivity.class);
startActivity(intent1);
//要求finish()或者startActivity(intent)方面后面执行
overridePendingTransition(R.anim.tran_in, R.anim.tran_out);
break;
布局代码:
在构造方法中,指定数据库的名称,版本号,游标工厂。
public BlackNumberDBOpenHelper(Context context) {
super(context, "blacknumber.db", null, 1);
}
/**
* 初始化数据库的表结构
*
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table blacknumber (_id integer primary key autoincrement,number varchar(20),mode varchar(2))");
}
升级数据库的方法先不管。整体的代码如下:
package com.ustc.mobilemanager.db;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class BlackNumberDBOpenHelper extends SQLiteOpenHelper {
/**
* 数据库构建的方法
*
* 数据库的名称:blacknumber.db
*
* @param context
*/
public BlackNumberDBOpenHelper(Context context) {
super(context, "blacknumber.db", null, 1);
}
/**
* 初始化数据库的表结构
*
*/
@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,用于对数据库的增删改查操作。在增删改查方法之前,必须先得到BlackNumberDBOpenHelper对象,并且在类的构造器中初始化它。
private BlackNumberDBOpenHelper helper;
/**
* 构造方法
*
* @param context
*/
public BlackNumberDao(Context context) {
// 实例化
helper = new BlackNumberDBOpenHelper(context);
}
package com.ustc.mobilemanager.db;
import java.util.ArrayList;
import java.util.List;
import com.ustc.mobilemanager.domain.BlackNumberInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
*
* 黑名单数据库的增删改差的业务类
*
* @author
*
*/
public class BlackNumberDao {
private BlackNumberDBOpenHelper helper;
/**
* 构造方法
*
* @param context
*/
public BlackNumberDao(Context context) {
// 实例化
helper = new BlackNumberDBOpenHelper(context);
}
/**
* 查询黑名单是否存在
*
*
* @param number
* @return
*/
public boolean find(String number) {
boolean result = false;
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from blacknumber where number=?",
new String[] { number });
while (cursor.moveToNext()) {
result = true;
}
cursor.close();
db.close();
return result;
}
/**
* 查询全部黑名单是否存在
*
*
* @param number
* @return
*/
public List findAll() {
List result = new ArrayList();
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db
.rawQuery("select number,mode from blacknumber", null);
while (cursor.moveToNext()) {
BlackNumberInfo info = new BlackNumberInfo();
String number = cursor.getString(0);
String mode = cursor.getString(1);
info.setMode(mode);
info.setNumber(number);
result.add(info);
}
cursor.close();
db.close();
return result;
}
/**
* 添加黑名单号码
*
* @param number
* 黑名单的号码
* @param mode
* 拦截模式 1.电话拦截2.短信拦截3.全部拦截
*/
public void add(String number, String mode) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("number", number);
values.put("mode", mode);
db.insert("blacknumber", null, values);
db.close();
}
/**
* 修改黑名单号码的拦截模式
*
* @param number
* 黑名单的号码
* @param newmode
* 新的拦截模式
*/
public void update(String number, String newmode) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("mode", newmode);
db.update("blacknumber", values, "number=?", new String[] { number });
db.close();
}
/**
* 删除黑名单号码
*
* @param number
* 黑名单的号码
*/
public void delete(String number) {
SQLiteDatabase db = helper.getWritableDatabase();
db.delete("blacknumber", "number=?", new String[] { number });
db.close();
}
}
在清单文件中,增加instrumentation结点。
helper.getWritableDatabase();这个语句创建了数据库的db文件。
package com.ustc.mobilemanager.test;
import java.util.List;
import java.util.Random;
import com.ustc.mobilemanager.db.BlackNumberDBOpenHelper;
import com.ustc.mobilemanager.db.BlackNumberDao;
import com.ustc.mobilemanager.domain.BlackNumberInfo;
import android.test.AndroidTestCase;
public class TestBlackNumberDB extends AndroidTestCase {
/**
* 测试创建数据库的方法
*
*/
public void testCreateDB() {
BlackNumberDBOpenHelper helper = new BlackNumberDBOpenHelper(
getContext());
helper.getWritableDatabase();
}
public void testAdd() {
BlackNumberDao dao = new BlackNumberDao(getContext());
long basenumber = 13512345;
Random random = new Random();
for (int i = 0; i < 100; i++) {
dao.add(String.valueOf(basenumber + i), String.valueOf(random.nextInt(3) + 1));
}
}
public void testUpdate() {
BlackNumberDao dao = new BlackNumberDao(getContext());
dao.update("110", "2");
}
public void testDelete() {
BlackNumberDao dao = new BlackNumberDao(getContext());
dao.delete("110");
}
public void testFind() {
BlackNumberDao dao = new BlackNumberDao(getContext());
boolean find = dao.find("110");
assertEquals(true, find);
}
public void testFindAll() {
BlackNumberDao dao = new BlackNumberDao(getContext());
List findAll = dao.findAll();
for (BlackNumberInfo blackNumberInfo : findAll) {
System.out.println(blackNumberInfo.toString());
}
}
}
可以使用:
需要在ListView的每个Item上显示电话号码和拦截模式,需要自定义Adapter,让其返回一个view。先看布局:
package com.ustc.mobilemanager.adapter;
import java.util.List;
import com.ustc.mobilemanager.R;
import com.ustc.mobilemanager.db.BlackNumberDao;
import com.ustc.mobilemanager.domain.BlackNumberInfo;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CallSmsSafeAdapter extends BaseAdapter {
private List infos;
private BlackNumberDao dao;
private Context context;
private TextView tv_black_number;
private TextView tv_black_mode;
public CallSmsSafeAdapter(Context context) {
this.context= context;
dao = new BlackNumberDao(context);
infos = dao.findAll();
}
@Override
public int getCount() {
return infos.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(context, R.layout.list_item_callsms, null);
tv_black_number = (TextView) view.findViewById(R.id.tv_black_number);
tv_black_mode = (TextView) view.findViewById(R.id.tv_black_mode);
tv_black_number.setText(infos.get(position).getNumber());
String mode = infos.get(position).getMode();
if ("1".equals(mode)) {
tv_black_mode.setText("电话拦截");
}else if ("2".equals(mode)) {
tv_black_mode.setText("短信拦截");
}else {
tv_black_mode.setText("全部拦截");
}
return view;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
设置设配器:
package com.ustc.mobilemanager;
import java.util.List;
import com.ustc.mobilemanager.adapter.CallSmsSafeAdapter;
import com.ustc.mobilemanager.db.BlackNumberDao;
import com.ustc.mobilemanager.domain.BlackNumberInfo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;
public class CallSmsSaveActivity extends Activity {
private ListView listView;
private List infos;
private BlackNumberDao dao;
private CallSmsSafeAdapter safeAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_call_sms_save);
dao = new BlackNumberDao(this);
safeAdapter = new CallSmsSafeAdapter(this);
infos = dao.findAll();
listView = (ListView) findViewById(R.id.list);
listView.setAdapter(safeAdapter);
}
}
运行: