eclipse可以使用Test實作代碼的局部測試
首先代碼清單檔案中需要添加途中檔案
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ldw.safe"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.ldw.safe" />
<application
android:allowBackup="true"
android:icon="@drawable/tb"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner" />
<activity
android:name="com.ldw.safe.Activity.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.ldw.safe.Activity.HomeActivity"></activity>
<activity android:name="com.ldw.safe.Activity.SettingActivity"></activity>
<activity android:name="com.ldw.safe.Activity.LostAndFind"></activity>
<activity android:name="com.ldw.safe.Activity.Setup1Activity"></activity>
<activity android:name="com.ldw.safe.Activity.Setup2Activity"></activity>
<activity android:name="com.ldw.safe.Activity.Setup3Activity"></activity>
<activity android:name="com.ldw.safe.Activity.Setup4Activity"></activity>
<activity android:name="com.ldw.safe.Activity.ContactActivity"></activity>
<activity android:name="com.ldw.safe.Activity.AdvancedToolsActivity"></activity>
<activity android:name="com.ldw.safe.Activity.AddressActivity"></activity>
<activity android:name="com.ldw.safe.Activity.DragViewActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
></activity>
<activity android:name="com.ldw.safe.Activity.CallSafeActivity"></activity>
<receiver android:name="com.ldw.safe.receiver.BootCompletedReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name="com.ldw.safe.receiver.SmsReceiver">
<intent-filter
android:priority="1000"
>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<!--
<receiver android:name="com.ldw.safe.receiver.OutCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
-->
<service android:name="com.ldw.safe.service.LocationService"></service>
<service android:name="com.ldw.safe.service.AddressService"></service>
</application>
</manifest>
TestBlackNumberDao.java這個檔案裡面是測試檔案,測試增删該查
package com.ldw.safe.test;
import java.util.List;
import java.util.Random;
import android.content.Context;
import android.test.AndroidTestCase;
import com.ldw.safe.bean.BlackNumberInfo;
import com.ldw.safe.db.dao.BlackNumberDao;
/**
*
*/
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());
}
}
}
黑名單的bean檔案BlackNumberInfo.java
package com.ldw.safe.bean;
/*
* 黑名單的javaBean
*/
public class BlackNumberInfo {
//number黑名單的号碼,
//mode黑名單的攔截模式1表示全部攔截2表示電話攔截3表示全部攔截
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;
}
}
建立資料庫表BlackNumberOpenHelper.java
package com.ldw.safe.db.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
/*
* 黑名單的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) {
// TODO Auto-generated method stub
}
}
資料庫相關的邏輯檔案BlackNumberDao.java實作資料庫的增删改查
package com.ldw.safe.db.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.ldw.safe.bean.BlackNumberInfo;
/*
* 黑名單資料庫的邏輯檔案
*/
public class BlackNumberDao {
private BlackNumberOpenHelper helper;
public BlackNumberDao(Context context){
helper = new BlackNumberOpenHelper(context);
}
//添加黑名單
//number黑名單号碼,mode攔截模式,"blacknumber"是表名
public boolean add(String number, String mode){
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("number", number);
contentValues.put("mode", mode);
long blacknumber = db.insert("blacknumber", null, contentValues);
if(blacknumber == -1){
//添加失敗
return false;
}else{
return true;
}
}
//通過電話号碼删除黑命單
//number電話号碼,"blacknumber"是表名
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 contentValues = new ContentValues();
contentValues.put("mode", mode);
int rowNumber = db.update("blacknumber", contentValues, "number=?", new String[]{number});
if(rowNumber == 0){
//修改失敗
return false;
}else{
return true;
}
}
//通過電話号碼查找黑名單攔截模式
public String findNumber(String number){
String mode = "";
SQLiteDatabase db = helper.getWritableDatabase();
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.getWritableDatabase();
List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
Cursor cursor = db.query("blacknumber", new String[]{"number", "mode"}, null, null, null, null, null);
//查詢所有的所有用while
while(cursor.moveToNext()){
BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
blackNumberInfo.setNumber(cursor.getString(0));
blackNumberInfo.setMode(cursor.getString(1));
blackNumberInfos.add(blackNumberInfo);
}
cursor.close();
db.close();
return blackNumberInfos;
}
}